(1)项目二:最大公约数和最小公倍数
#include<iostream.h>
int fun(int m,int n,int &gcd)
{
int a=m,b=n,t;
if(a<b)
{
t=a;
a=b;
b=t;
}
gcd=b;
while(a%b)
{
gcd=a%b;
a=b;
b=gcd;
}
return (m*n/gcd);
}
void main()
{
int m,n,k;
cout<<"输入两个整数:";
cin>>m>>n;
cout<<"这两个数的最小公倍数:"<<fun(m,n,k)<<endl;
cout<<"这两个数的最大公约数:"<<k<<endl;
}(2)项目三:特殊三位数
#include<iostream.h>
int f(int x)
{
int fact=1;
for(int i=1;i<=x;i++)
{
fact*=i;
}
return fact;
}
int main()
{
int i,a,b,c;
cout<<"三位数中所有的特殊三位数:";
for(i=100;i<1000;i++)
{
a=i/100;
b=i/10%10;
c=i%10;
if(i==f(a)+f(b)+f(c))
cout<<i<<" ";
}
cout<<endl;
return 0;
}
本文通过C++实现两个经典数学问题:一是求解任意两整数的最大公约数及最小公倍数;二是找出所有满足条件的特殊三位数,这些数等于其各位数字阶乘之和。
678

被折叠的 条评论
为什么被折叠?



