第四周项目1-求最大公约数(1)

来源:互联网 发布:淘宝买家号查询 编辑:程序博客网 时间:2024/05/17 09:15

/*2015,烟台大学计算机与控制工程学院 

 *All rightreserved. 

 *文件名称:test.cpp 

 *作   者:张明宇

 *完成日期:2016年3月20日 

 */

问题及代码:

输入两个数,并求出其最大公约数

#include<iostream>using namespace std;int gcd(int a,int b);int main(){    int a,b,g;    cin>>a>>b;    g=gcd(a,b);    cout<<"最大公约数是:"<<g;    return 0;}int gcd(int a,int b){    int t,r;    if(a<b)    {        t=a;        a=b;        b=t;    }    while(r!=0)    {        r=a%b;        a=b;        b=r;    }    return a;}

学习心得:C++的简单运用;

0 0
原创粉丝点击