第九次作业

1.随机产生一个0-99的数,猜猜看,如果大了,就显示大了点;如果小了,就显示小了点,直到猜对为止。

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int a,n;
srand((unsigned)time(NULL));
a=rand()%100;
printf("%d\n",a);
while(1){
scanf("%d",&n);
if(n>a)
printf("大了点\n");
else if(n==a)
printf("正确");
else
printf("小了点\n");
}
}

 

 

 

2.编写程序,求1-1/2+1/3-1/4+......+1/99-1/100的值。 

#include<stdio.h>
main()
{
int a,b;
float p,s,h;
p=0.0;
a=0.0;
for(a=1;a<=99;a+=2)
p+=1.0/a;
for(b=2;b<=100;b+=2)
s+=1.0/b;
h=p-s;
printf("h=%f\n",h);
}

 

 

3.编写程序,求s=1+(1+2)+(1+2+3)+......+(1+2+3+.....n)的值。

#include<stdio.h>
main()
{
int a,s,n,i;
scanf("%d",&n);
a=0;
s=0;
for(i=1;i<=n;i++)
{
a+=i;
s=a+s;
}
printf("%d",s);
}

 

 

4.编写程序,用户输入一个正整数,把它的各位数字前后颠倒并输出颠倒后的结果。

#include<stdio.h>
main()
{
int a,b;
printf("请输入一个数:\n");
scanf("%d",&a);
while(a!=0)
{
b=a%10;
printf("%d",b);
a/=10;
}
}

 

 

5.100匹马驮100担货,打匹马一匹驮3担,中匹马一匹驮2担,小匹马一匹驮1担,试编写程序,计算大中小马的数目。

#include<stdio.h>
main()
{
int x,y,z;
for(x=0;x<=100;x++)
{
for(y=0;y<=100;y++)
for(z=0;z<=100;z++)
if(x+y+z==100)
if(3*x+2*y+z*0.5==100)
printf("大匹马:%d,中匹马:&d,小匹马:%d\n",x,y,z);
}
}

 

 

 

posted @ 2021-11-20 17:43  coldlane  阅读(2)  评论(0编辑  收藏  举报