期末考试-最大公约数(算法基础 第10周)

来源:互联网 发布:知名网络小说家 编辑:程序博客网 时间:2024/06/07 09:38

问题描述:
这里写图片描述
分析
辗转相除法
源码

#include <iostream>using namespace std;int fun(int& a, int& b) {    int temp;    while(b){        temp = a%b;        a=b;        b=temp;    }    return a;}int main() {    int a, b;    while(cin>>a>>b) {        if (a>b) {            cout << fun(a, b) << endl;        }        else {            cout << fun(b, a) << endl;        }    }    return 0;}
0 0
原创粉丝点击