#include <stdio.h>int hcf(int u,int v)//求最大公約數。 { int r; while((r=u%v)!=0)//輾轉除法
uj5u.com熱心網友回復:
供參考:#include <iostream.h>
#include <stdio.h>
int gys(int x,int y) //輾轉相除法 公約數
{
int a;
if(x<y) //將大的數排在前面
{
a=x;
x=y;
y=a;
}
while(x%y!=0)
{
a=x%y;
x=y;
y=a;
}
return y;
}
int gbs(int x,int y) //公倍數
{
int result=(x*y)/(gys(x,y));
return result;
}
int main(int argc, char* argv[])
{
int x,y;
cout<<"請輸入兩個正整數:";
cin>>x>>y;
cout<<"最大公約數為:" <<gys(x,y)<<endl;
cout<<"最小公倍數為:" <<gbs(x,y)<<endl;
system("pause");
return 0;
}
uj5u.com熱心網友回復:
網上對于這樣的代碼和需求還是挺多的,建議自己動手查查,測驗試試~轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/234392.html
標籤:C語言
上一篇:求助
下一篇:用vc6.0撰寫學生成績管理系統
