3.1源码:#include <stdio.h>
int main()
{
int i,sum=0;
i=1;
while(i<=100)
{
sum+=i;
i++;
}
printf("sum=%d\n",sum);
return 0;
}#include <stdio.h>
int main()
{
int i,sum=0;
do{
sum+=i;
i++;
}
while(i<=100);
printf("sum=%d\n",sum);
return 0;
}#include <stdio.h>
int main()
{
int i,sum=0;
for(i=1;i<=100;i++)
sum+=i;
printf("sum=%d\n",sum);
return 0;
}
运行结果抓图
3.2源码#include <stdio.h>
int main()
{
int i,n,nj,sum=1;
printf("putin n:");
scanf("n=%f",&n);
while(i<=n)
{
sum+=i;
i++;
nj=sum*i;
}
if(i=n)
printf("n!=%f\n",nj);
return 0;
}
3.3程序源码#include<stdio.h>
int main(){
int k,h;
int sum=0,sum1=0,sum2=0;
for(k=1;k<=100;k++)
sum+=k;
printf("sum=%d\n",sum);
for(k=1;k<=50;k++)
sum1=sum1+k*k;
printf("sum1=%d\n",sum1);
for(k=1;k<=10;k++)
sum2+=1/k;
printf("sum2=%d\n",sum2);
h=sum+sum1+sum2;
printf("结果为:%d\n",h);
return 0;
3.4程序源码#include <stdio.h>
int main()
{
char c;
int zm=0,sz=0,kg=0,qt=0;
while((c=getchar())!=‘\n‘)
{
if((c>=‘a‘&& c<=‘z‘)||(c>=‘A‘&& c<=‘Z‘))
zm++; else if(c==‘ ‘)
kg++;
else if(c>=‘0‘&&c<=‘9‘)
sz++;
else
qt++;
}
printf("the number of letter:%d\n",zm);
printf("空格个数;%d\n",kg);
printf("数字个数;%d\n",sz);
printf("其他个数;%d\n",qt);
return 0;
}
3.5程序源码#include<stdio.h>
int main(){
int m,n,t,i;
printf("请输入两个整数:");
scanf("%d%d",&m,&n);
if(n>m){
t=m;
m=n;
n=t;
}
for(i=n;i>1;i--)
if(m%i==0&&n%i==0)
break;
printf("m=%d,n=%d",m,n);
if(i!=0)
printf(",最大公因数为:%d",i);
t=(m*n)/i;
printf("最小公倍数为: %d",t);
return 0;
}
3.6程序源码#include <stdio.h>
int main(void)
{
int a,b,c;
for(a=1;a<=9;a++)
for(b=0;b<=9;b++)
for(c=0;c<=9;c++)
if(100*a+10*b+c==a*a*a+b*b*b+c*c*c)
printf("%d%d%d\n",a,b,c);
return 0;
}
实验心得
结构大体清楚,但题目里的算法和意思不太懂。
时间: 2024-12-28 01:33:07