프로그래밍/알고리즘

[Algorithm] 백준 온라인 저지 문제 풀이

AlgorFati 2021. 4. 12. 13:35

별찍기 - 7

www.acmicpc.net/problem/2444

 

2444번: 별 찍기 - 7

첫째 줄부터 2×N-1번째 줄까지 차례대로 별을 출력한다.

www.acmicpc.net

 

소스코드

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    // 별찍기 - 7

    int n;
    cin >> n;
    // scanf

    // 5가 들어왔을 경우 -4 ~ 4

    for (int y = -(n - 1); y <= n - 1; ++y)
    {
        for (int x = -(n - 1); x <= n - 1; ++x) // 총 2*n - 1 번
        {
            if (abs(x) + abs(y) <= n - 1)
            {
                cout << "*";
            }
            else
            {
                if (x < 0)
                {
                    cout << " ";
                }

            }

        }
        if (y != n - 1)
        {
            cout << endl;
        }
    }
    
    // |x| + |y| = 1


    return 0;
}

 

OX 퀴즈

www.acmicpc.net/problem/8958

 

8958번: OX퀴즈

"OOXXOXXOOO"와 같은 OX퀴즈의 결과가 있다. O는 문제를 맞은 것이고, X는 문제를 틀린 것이다. 문제를 맞은 경우 그 문제의 점수는 그 문제까지 연속된 O의 개수가 된다. 예를 들어, 10번 문제의 점수

www.acmicpc.net

 

소스코드

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        string str;
        cin >> str;

        int sum = 0;
        int cnt = 0;
        for (auto c : str)
        {
            if (c == 'O')
            {
                cnt++;
            }
            else
            {
                cnt = 0;
            }

            sum += cnt;
        }
        cout << sum << endl;
    }

    return 0;
}

 

 

바구니 뒤집기

www.acmicpc.net/problem/10811

 

10811번: 바구니 뒤집기

도현이는 바구니를 총 N개 가지고 있고, 각각의 바구니에는 1번부터 N번까지 번호가 순서대로 적혀져 있다. 바구니는 일렬로 놓여져 있고, 가장 왼쪽 바구니를 1번째 바구니, 그 다음 바구니를 2

www.acmicpc.net

 

소스코드

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

void print(const vector<int>& v)
{
    for (int i = 1; i < v.size(); ++i)
    {
        cout << v[i] << " ";
    }
    cout << endl;
}

void reverse(vector<int>& v, int from, int to)
{
    std::reverse(begin(v) + from, begin(v) + to + 1);
}

int main() 
{    
    int n, m;
    
    cin >> n >> m;
    vector<int> v(n + 1);

    for (int i = 1; i <= n; ++i)
    {
        v[i] = i;
    }
    
    for (int i = 0; i < m; ++i)
    {
        int from, to;
        cin >> from >> to;

        reverse(v, from, to);
    }
    
    print(v);

    return 0;
}


 

 

미로 탐색

www.acmicpc.net/problem/2178

 

2178번: 미로 탐색

첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.

www.acmicpc.net

 

소스코드

github.com/insooneelife/AlgorithmTutorial/blob/master/Algorithm/Sources/StateSpaceSearch/Solutions/Baekjoon/%EB%AF%B8%EB%A1%9C%20%ED%83%90%EC%83%89.cpp

 

insooneelife/AlgorithmTutorial

알고리즘 튜토리얼. Contribute to insooneelife/AlgorithmTutorial development by creating an account on GitHub.

github.com

 

 

 

정수 삼각형

www.acmicpc.net/problem/1932

 

1932번: 정수 삼각형

첫째 줄에 삼각형의 크기 n(1 ≤ n ≤ 500)이 주어지고, 둘째 줄부터 n+1번째 줄까지 정수 삼각형이 주어진다.

www.acmicpc.net

 

소스코드

github.com/insooneelife/AlgorithmTutorial/blob/master/Algorithm/Sources/DP/Solutions/Baekjoon/%EC%A0%95%EC%88%98%20%EC%82%BC%EA%B0%81%ED%98%95.cpp

 

insooneelife/AlgorithmTutorial

알고리즘 튜토리얼. Contribute to insooneelife/AlgorithmTutorial development by creating an account on GitHub.

github.com