第九次作业
#include<stdio.h> #include <stdlib.h> #include <time.h> int main() { while(1) { int a=0,b; srand((unsigned)time(NULL)); a = rand()%100+1; printf("请输入一个数"); scanf("%d",&b); if(a>b){ printf("小了"); } if(a<b){ printf("大了"); } printf("\n"); } }
2.编写一个程序,求值
#include <stdio.h> main() { int a=0,b; double sum=0; for(b=1;b<=100;b++) { if(b%2==0) { a=b*(-1); } else { a=b; } sum=sum+1.0/a; } printf("%f",sum); }
3.编写一个程序,求值
#include <stdio.h> main() { int s=0,n,c,b=0; printf("请输入一个整数"); scanf("%d",&n); for(c=1;c<=n;c++) { b+=c; s=s+b; } printf("%d",s); }
`
(3)编写一个程序,用户输入一个正整数,把它的各位数字前后颠倒一下,并输出颠倒后的结果。
#include <stdio.h> main() { int n,m,s=0; printf("请输入一个正整数"); scanf("%d",&n); while(n>0) { m=n%10; s=s*10+m; n/=10; } printf("%d\n",s); }
(4)100匹马驮100担货,大马一匹驮3担,中马一匹驮2担,小马两匹驮1担。试编写程序计算大、中、小马的数目
//100匹马驮100担货,大马一匹驮3担,中马一匹驮2担,小马两匹驮1担。试编写程序计算大、中、小马的数目。 #include <stdio.h> main() { int d,z,x,dongxi; for(d=0;d<=33;d++) { for(z=0;z<=50;z++) { x=100-d-z; if(x%2!=0) { continue; } dongxi=d*3+z*2+x/2; if(dongxi==100) printf("大马%d匹,中马%d匹,小马%d匹\n",d,z,x); } } }