最大公约数 poj

来源:互联网 发布:廖雪峰python教程微盘 编辑:程序博客网 时间:2024/05/17 08:32

只需利用初等数论的知识——辗转相除法即可

#include <iostream>#include <cmath>#include <climits>#include <string>#include <cctype>#include <vector>#include <cmath>#include <cstring>#include <array>using namespace std;int main(){    int i,j,k,m,n,N,M;    while(cin>>n>>m){        int temp,maxp;   //n>=m        if(m>n){            temp=m;            m=n;            n=temp;        }        N=n;        M=m;        if(n%m==0) maxp=m;        else{            k=n%m;            while(M%k!=0 ||N%k!=0){                n=m;                m=k;                k=n%m;            }            maxp=k;        }        cout<<maxp<<endl;    }}


0 0
原创粉丝点击