C语言实验问题

2025-03-25 23:30:15
推荐回答(3个)
回答1:

一定要采用啊!化很长时间的:

第一题:
#include
int mul(int M,int N)
{
if(N==0)
return M;
return mul(N,M%N);
}
void main()
{
cout<<"输入两个数:";
int M,N;
cin>>M>>N;
int z=mul(M,N);
cout<<"最大公约数:"<}

第二题:
#include
void main()
{
int m,s,i;
for(m=2;m<1000;m++)
{
s=0;
for(i=1;i if(m%i==0)
s=s+i;
if(s==m)
{
printf("%3d,its factors are",m);
for(i=1;i if(m%i==0)
printf("%5d",i);
printf("\n");
}

}
}

第三题:
#include
#include
void main()
{
float x1,x0,f,f1;
x1=1.5;
do
{
x0=x1;
f=((2*x0-4)*x0+3)*x0-6;
f1=(6*x0-8)*x0+3;
x1=x0-f/f1;
}while(fabs(x1-x0)>1e-5);
printf("the root of equation is %5.2f\n",x1);
}
第四题:
第一个:
#include
void main()
{
int day,x1,x2;
day=9;
x2=1;
while(day>0)
{
x1=(x2+1)*2;
x2=x1;
day--;
}
printf("total=%d\n",x1);
}

第二个:
#include
void main()
{
int day,x1,x2;
day=9;
x2=1;
while(day>0)
{
x1=(x2+2)*2;
x2=x1;
day--;
}
printf("total=%d\n",x1);
}

回答2:

输入两个正整数m和n, 求其最大公约数和最小公倍数.

<1> 用辗转相除法求最大公约数
算法描述:
m对n求余为a, 若a不等于0
则 m <- n, n <- a, 继续求余
否则 n 为最大公约数
<2> 最小公倍数 = 两个数的积 / 最大公约数

#include
int main()
{
int m, n;
int m_cup, n_cup, res; /*被除数, 除数, 余数*/
printf("Enter two integer:\n");
scanf("%d %d", &m, &n);
if (m > 0 && n >0)
{
m_cup = m;
n_cup = n;
res = m_cup % n_cup;
while (res != 0)
{
m_cup = n_cup;
n_cup = res;
res = m_cup % n_cup;
}
printf("Greatest common divisor: %d\n", n_cup);
printf("Lease common multiple : %d\n", m * n / n_cup);
}
else printf("Error!\n");
return 0;
}

-----------------------猴子吃桃-------------------------
#include
main( )
{
int i, peach;
peach=1;
printf("Enter i:");
scanf("%d",&i);
while(i>1)
{
i--;
peach=(peach+1)*2;
}
printf("%d\n", peach);
return 0;
}

回答3:

留个联系方式吧,正好最近在研究算法,做做看。
算了,这些还是你自己做吧,题都很简单,你自己应该得做得来