백준 14490 백대열

치즈·2023년 3월 6일

BOJ

목록 보기
43/45

#수학 #문자열 #정수론 #유클리드 호제법

#include <iostream>
#include <vector>
#include <sstream>
#include <queue>
#include <string>
using namespace std;

string s;

vector<string> split(string input, char deli) {
    vector<string> result;
    stringstream ss(input);
    string tmp;
 
    while (getline(ss, tmp, deli)) result.push_back(tmp);
 
    return result;
}
int a[2];
void input(){
  cin >> s;
  vector<string> ret = split(s, ':');
  for(int i = 0; i < ret.size(); i++){
    a[i] = stoi(ret[i]);
  }
}

int gcd(int a, int b){
  if(a < b){
    int tmp = a;
    a = b;
    b = tmp;
  }
  if(a == b) return a;
  if(a > b) return gcd(a - b, b);

}

void solve(){
  int common = gcd(a[0], a[1]);
  cout << a[0]/common << ":" <<a[1]/common;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  input();
  solve();
  
  return 0;
}

profile
차근차근 배워나가요

0개의 댓글