본문 바로가기
코딩테스트/백준

[백준] 단계별로 풀어보기 > for문 (c++)

by Hwan,. 2022. 1. 13.
728x90
반응형

문제 1. 구구단

#include <iostream>

using namespace std;

int main() {
    int a;

    cin >> a;

    for (int i=1; i<10; i++) {
        cout << a << " * " << i << " = " << a * i << endl;
    }

    return 0;
}

 

문제 2. A+B - 3

#include <iostream>

using namespace std;

int main() {
    int cnt=0, a, b;

    cin >> cnt;
    int *arr = new int[cnt];

    for (int i = 0; i < cnt; i++) {
        cin >> a >> b;
        arr[i] = a + b;
    }

    for (int i = 0; i < cnt; i++) {
        cout << arr[i] << endl;
    }

    return 0;
}

 

문제 3. 합

#include <iostream>

using namespace std;

int main() {
    int a, sum=0;

    cin >> a;

    for (int i = 1; i <= a; i++) {
        sum += i;
    }

    cout << sum;

    return 0;
}

 

문제 4. 빠른 A+B

#include <iostream>

using namespace std;

int main() {
    int cnt = 0, a, b;

    cin.tie(NULL);
    cin.sync_with_stdio(false);

    cin >> cnt;
    int* arr = new int[cnt];

    for (int i = 0; i < cnt; i++) {
        cin >> a >> b;
        arr[i] = a + b;
    }

    for (int i = 0; i < cnt; i++) {
        cout << arr[i] << "\n";
    }

    return 0;
}

 

문제 5. N 찍기

#include <iostream>

using namespace std;

int main() {
    int cnt = 0;

    cin.tie(NULL);
    cin.sync_with_stdio(false);

    cin >> cnt;

    for (int i = 1; i <= cnt; i++) {
        cout << i << "\n";
    }

    return 0;
}

 

문제 6. 기찍 N

#include <iostream>

using namespace std;

int main() {
    int cnt = 0;

    cin.tie(NULL);
    cin.sync_with_stdio(false);

    cin >> cnt;

    for (int i = cnt; i > 0; i--) {
        cout << i << "\n";
    }

    return 0;
}

 

문제 7. A+B - 7

#include <iostream>

using namespace std;

int main() {
    int cnt = 0, a, b;

    cin >> cnt;
    int* arr = new int[cnt];

    for (int i = 0; i < cnt; i++) {
        cin >> a >> b;
        arr[i] = a + b;
    }

    for (int i = 0; i < cnt; i++) {
        cout << "Case #" << i+1 << ": "<< arr[i] << endl;
    }

    return 0;
}

 

문제 8. A+B - 8

#include <iostream>

using namespace std;

int main() {
    int cnt = 0;

    cin >> cnt;
    int* arr = new int[cnt];
    int* a = new int[cnt];
    int* b = new int[cnt];

    for (int i = 0; i < cnt; i++) {
        cin >> a[i] >> b[i];
        arr[i] = a[i] + b[i];
    }

    for (int i = 0; i < cnt; i++) {
        cout << "Case #" << i+1 << ": " << a[i] << " + " << b[i] << " = " << arr[i] << endl;
    }

    return 0;
}

 

문제 9. 별 찍기 - 1

#include <iostream>

using namespace std;

int main() {
    int cnt = 0;

    cin >> cnt;

    for (int i = 0; i < cnt; i++) {
        for (int j = 0; j <= i; j++) {
            cout << "*";
        }
        cout << "\n";
    }

    return 0;
}

 

문제 10. 별 찍기 - 2

#include <iostream>

using namespace std;

int main() {
    int cnt = 0;

    cin >> cnt;

    for (int i = 1; i <= cnt; i++) {
        for (int j = cnt; j >= 1; j--) {
            if (i < j) {
                cout << " ";
            }
            else {
                cout << "*";
            }
        }
        cout << "\n";
    }

    return 0;
}

 

문제 11. X보다 작은 수

#include <iostream>

using namespace std;

int main() {
    int n, x;
    cin >> n >> x;

    int* arr = new int[n];

    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    for (int i = 0; i < n; i++) {
        if (arr[i] < x) {
            cout << arr[i];
            if (i != n - 1) {
                cout << " ";
            }
        }
    }


    return 0;
}

 

결과

728x90
반응형

댓글