본문 바로가기

PS

[ 백준 / C++ ] 14490 : 백대열

[ 문제 ]

 

14490번: 백대열 (acmicpc.net)

 

[ 접근방법 ]

 

find 함수를 통해 string 안에 원하는 문자의 위치를 찾을 수 있다.

 

substr 함수를 통해 string의 원하는 부분을 추출할 수 있다.

 

stoi 함수를 통해 string을 int로 변환할 수 있다.

 

[ 소스코드 ]

 

#include <iostream>
#include <string>

using namespace std;

int n, m, g;

int gcd(int a, int b)
{
    if (a < b)
        return gcd(b, a);
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    string str;
    cin >> str;

    n = stoi(str.substr(0, str.find(":")));
    m = stoi(str.substr(str.find(":") + 1, str.length()));

    g = gcd(n, m);

    n /= g;
    m /= g;

    cout << n << ":" << m;

    return 0;
}

'PS' 카테고리의 다른 글

[ 백준 / C++ ] 5430 : AC  (0) 2024.06.12
[ 백준 / C++ ] 12904 : A와 B  (0) 2024.06.11
[ 백준 / C++ ] 15486 : 퇴사 2  (0) 2024.06.05
[ 백준 / C++ ] 1024 : 수열의 합  (0) 2024.06.05
[ 백준 / C++ ] 1285 : 동전 뒤집기  (0) 2024.05.31