1. 임의의 두 정수 num1과 num2를 입력받는다.
2. num2가 0이면 num1이 최대공약수이다.
3. num2가 0이 아니면 num1에 num2를, num2에 num1%num2를 대입하여 다시 Euclid Function Call
#include <iostream>
using namespace std;
int Euclid (int num1, int num2){ //유클리드 알고리즘을 구현한 함수
if(num2 == 0)
return num1;
else
return Euclid(num2, num1 % num2);
}
void main()
{
int num1, num2;
cout<<"두 수를 입력하시오."<<endl;
cin >> num1 >> num2;
cout << Euclid(num1, num2)<< endl;
}
'SW > 알고리즘' 카테고리의 다른 글
Tree Traversal1 - Preorder Traversal (0) | 2019.02.23 |
---|---|
Right Hand Rule (0) | 2018.12.30 |
Eratosthenes's Sieve (0) | 2018.12.30 |
Prime Number (0) | 2018.12.24 |
Asymptotic Analysis (0) | 2018.12.23 |