약분하는 문제입니다.
x, y를 입력받고 x, y를 div로 나누어 떨어지는지 확인하고
아닐시 div를 1감소하고 나누어 떨어질때까지 반복합니다.
(x > y div = y, x < y div = x)
나누어 떨어질 경우 나누어 떨어진 값을 출력합니다.
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
int x, y;
int div;
int i = 0;
string s;
string temp;
cin >> s;
while (1) {
if (s[i] == ':') {
temp = s.substr(0, i);
x = stoi(temp);
temp = s.substr(i + 1, s.length());
y = stoi(temp);
break;
}
++i;
}
div = min(x, y);
while(x % div != 0 || y % div != 0) {
--div;
}
cout << x / div << ":" << y / div << endl;
return 0;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 5337번 웰컴 (0) | 2019.09.18 |
---|---|
[C++] 백준 2903번 중앙 이동 알고리즘 (0) | 2019.09.18 |
[C++] 백준 2959번 거북이 (0) | 2019.09.17 |
[C++] 백준 5052번 전화번호 목록 (0) | 2019.09.16 |
[C++] 백준 11652번 카드 (0) | 2019.09.16 |