JXNVCE ALGO-LOG YouJin Jung

MAGIC SQUARE

Magic square applied for odd numbers from geeks for geeks practice

#include <bits/stdc++.h>
using namespace std;
 
// A function to generate odd sized magic squares
void generateSquare(int n)
{
    int magicSquare[n][n];
 
    // set all slots as 0
    memset(magicSquare, 0, sizeof(magicSquare));
 
    // Initialize position for 1
    int i = n / 2;
    int j = n - 1;
 
    // One by one put all values in magic square
    for (int num = 1; num <= n * n;) {
        if (i == -1 && j == n) // 3rd condition
        {
            j = n - 2;
            i = 0;
        }
        else {
            // 1st condition helper if next number
            // goes to out of square's right side
            if (j == n)
                j = 0;
 
            // 1st condition helper if next number
            // is goes to out of square's upper side
            if (i < 0)
                i = n - 1;
        }
        if (magicSquare[i][j]) // 2nd condition
        {
            j -= 2;
            i++;
            continue;
        }
        else
            magicSquare[i][j] = num++; // set number
 
        j++;
        i--; // 1st condition
    }
 
    // Print magic square
    cout << "The Magic Square for n=" << n
         << ":\nSum of "
            "each row or column "
         << n * (n * n + 1) / 2 << ":\n\n";
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++)
 
            // setw(7) is used so that the matrix gets
            // printed in a proper square fashion.
            cout << setw(4) << magicSquare[i][j] << " ";
        cout << endl;
    }
}
 
int main() {
    // Works only when n is odd
    int n = 7;
    generateSquare(n);
    return 0;
}

GFG MOSER DE BRUJIN SEQUENCE

해당 시퀀스의 룰

1) S(2 * n) = 4 * S(n)
2) S(2 * n + 1) = 4 * S(n) + 1
with S(0) = 0 and S(1) = 1
#include <bits/stdc++.h>
using namespace std;

int gen(int n) {
    int S[n+1];
    S[0] = 0;
    S[1] = 1;
    for (int i = 2; i <= n; i++) {   
        // S(2 * n) = 4 * S(n)
        if (i % 2 == 0)
           S[i] = 4 * S[i / 2];
     
        // S(2 * n + 1) = 4 * S(n) + 1
        else
           S[i] = 4 * S[i / 2] + 1;
    }
    return S[n];
}

void moserDeBruijn(int n)  {
    for (int i = 0; i < n; i++)
        cout << gen(i) << " ";
    cout << endl;
}
 
int main() {
    int n = 7;
    cout << "First " << n << " terms of " << "Moser-de Bruijn Sequence : " << endl;
    moserDeBruijn(n);
    return 0;
}

GFG NEWMAN CONWAY SEQUENCE

P(n) = P(P(n - 1)) + P(n - P(n - 1)) 

1 1 2 2 3 4 4 4 5 6 7 7… Recurence Correlation

#include <bits/stdc++.h>
using namespace std;
 
int sequence(int n) {

    int f[n + 1];
    int i;
    f[0] = 0;
    f[1] = 1;
    f[2] = 1;
 
    for (i = 3; i <= n; i++)
        f[i] = f[f[i - 1]] + f[i - f[i - 1]];   
 
    return f[n];
}
 
int main() {
    int n = 10;
    cout << sequence(n);
    return 0;
}

BOJ-10821 정수의 개수

#include <iostream>
#include <string>

using namespace std;

int main() {
	string str;
	getline(cin, str, '\n');

	int count = 0;

	for (int i = 0; i < str.size(); i++) {
		if (str[i] == ',') count++;
	}
	cout << count + 1;
}

CHECK FOR INTEGER OVERFLOW

#include <bits/stdc++.h>
using namespace std;
  
int addOvf(int* result, int a, int b)  { 
    *result = a + b; 
    if(a > 0 && b > 0 && *result < 0) 
        return -1; 
    if(a < 0 && b < 0 && *result > 0) 
        return -1; 
    return 0; 
} 
  
int main()  { 
    int *res = new int[(sizeof(int))]; 
    int x = 2147483640; 
    int y = 10; 
  
    cout << addOvf(res, x, y); 
    cout << "\n" <<*res; 
    return 0; 
}