JXNVCE ALGO-LOG YouJin Jung

2022-02-14 BOJ 1105

#include<iostream>

using namespace std;

int main() {
	ios::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  
	string a, b;
	int cnt = 0;
  cin >> a >> b; 
  
	if (a.length() != b.length()) {
		cout << 0;
		return 0;
	}

	for (int i = 0; i < a.length(); i++) {
		if (a[i] == b[i] && a[i] == '8') cnt++;
		else if (a[i] != b[i]) break;
	}
  
	cout << cnt;
	return 0;
}

GFG EXPONENTIAL CALCULATION EFFICIENTLY!

Way to calcuate e^x efficiently… 💪🏼 => Taylor Series

#include <bits/stdc++.h>
using namespace std;
 
float exponential(int n, float x) {
    float sum = 1.0f;
    for (int i = n - 1; i > 0; --i )
        sum = 1 + x * sum / i;
    return sum;
}
 
int main() {
    int n = 10;
    float x = 1.0f;
    cout << "e^x = " << fixed << setprecision(5) << exponential(n, x);
    return 0;
}

GFG RANDOM NUMBER FROM STREAM

#include <bits/stdc++.h>
#include <time.h>

using namespace std;

int selectRandom(int x) {
    static int res; 
    static int count = 0; 
    count++; 
    if (count == 1) res = x;
    else {
        int i = rand() % count;
        if (i == count - 1)
            res = x;
    }
    return res;
}
 
int main() {
    int stream[] = {1, 2, 3, 4};
    int n = sizeof(stream) / sizeof(stream[0]);

    srand(time(NULL));
    for (int i = 0; i < n; ++i)
        cout << "Random number from first " << i + 1 << " numbers is " << selectRandom(stream[i]) << endl;
    return 0;
}

GFG DFA BASED DIVISION

#include <bits/stdc++.h>

using namespace std;

void preprocess(int k, int Table[][2]) {
    int trans0, trans1;
    for (int state = 0; state < k; ++state) {
        trans0 = state << 1;
        Table[state][0] = (trans0 < k) ? trans0 : trans0 - k;
        trans1 = (state << 1) + 1;
        Table[state][1] = (trans1 < k) ? trans1 : trans1 - k;
    }
}

void isDivisibleUtil(int num, int* state, int Table[][2]) {
    if (num != 0) {
        isDivisibleUtil(num >> 1, state, Table);
        *state = Table[*state][num & 1];
    }
}

int isDivisible (int num, int k) {
    int (*Table)[2] = (int (*)[2])malloc(k*sizeof(*Table));
    preprocess(k, Table);
    int state = 0;
    isDivisibleUtil(num, &state, Table);
    return state;
}
 
int main() {
    int num = 47;
    int k = 5;
    int remainder = isDivisible (num, k);
    if (remainder == 0)
        cout << "Divisible\n";
    else
        cout << "Not Divisible: Remainder is " << remainder;
 
    return 0;
}

GFG MINIMUM NUMBER OF SQUARES

예를들면, 100 = 10^2 = 5^2 + 5^2 + 5^2 + 5^2 인데, 이런 식의 minimum number of 제곱을 구해볼 수 있다. => recursive 방식

#include <bits/stdc++.h>
using namespace std;

int getMinSquares(unsigned int n) {
    if (sqrt(n) - floor(sqrt(n)) == 0)
        return 1;
    if (n <= 3)
        return n;
        
    int res = n;
    for (int x = 1; x <= n; x++) {
        int temp = x * x;
        if (temp > n)
            break;
        else
            res = min(res, 1 + getMinSquares(n - temp));
    }
    return res;
}
 
int main() {
    cout << getMinSquares(6);
    return 0;
}