问题及代码:
/*
*Copyright (c) 2015,烟台大学计算机学院
*All rights reserved.
*文件名称:text.cpp
*作者:徐健
*完成日期:2015年3月22日
*版本号:v1.0
*
*问题描述:求最大公约数
*输入描述:输入2个数值
*程序输出:输出1个数值,为最大公约数
*/
#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 c;
while(b!=0)
{
c=a%b;
a=b;
b=c;
}
return a;
}
/*
*Copyright (c) 2015,烟台大学计算机学院
*All rights reserved.
*文件名称:text.cpp
*作者:徐健
*完成日期:2015年3月22日
*版本号:v1.0
*
*问题描述:求最大公约数
*输入描述:输入4个数值
*程序输出:输出1个数值,为最大公约数
*/
#include <iostream>
using namespace std;
int gcd(int a,int b);
int main()
{
int a,b,g,c,d;
cin>>a>>b>>c>>d;
g=gcd((a,b),(c,d));
cout<<"最大公约数是:"<<g;
return 0;
}
int gcd(int x,int y)
{
int m;
while(y!=0)
{
m=x%y;
x=y;;
y=m;
}
return x;
}
运行结果:
知识点总结:
运用了while循环,以及自定义函数的运用。
学习心得:
编程时要注意对于现有资源有效的利用,这样不仅可以节省时间,更可以使程序看起来更加的井井有条,可视化程度更高。