본문 바로가기

Beakjoon/math

[백준] 1850번 최대공약수 (C++) - Euclidean algorithm

문제

https://www.acmicpc.net/problem/1850

 

1850번: 최대공약수

모든 자리가 1로만 이루어져있는 두 자연수 A와 B가 주어진다. 이때, A와 B의 최대 공약수를 구하는 프로그램을 작성하시오. 예를 들어, A가 111이고, B가 1111인 경우에 A와 B의 최대공약수는 1이고, A

www.acmicpc.net

코드

#include <iostream>

using namespace std;

long long gcd(long long a, long long b){
  while(b!=0){
    long long c=a%b;
    a=b;
    b=c;
  }
  return a;
}

int main(){
  long long a,b;
  cin >> a >> b;
  int num=gcd(a,b);

  for(int i=0; i<num; i++){
    cout << '1';
  }
  return 0;
}

정리

 

참조

https://fishersheep.tistory.com/182