第十一次作业

1.猜数字,猜错提示,猜对结束。

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main() {

    int a,b,c;

    srand((unsigned)time(NULL));

    a = rand();

printf("%d\n", a);

b = rand()%100;

    printf("%d\n", b);

do{

     scanf("%d",&c);

if(c<b)

printf("小了点,请重新输入");

else if(c>b)

    printf("大了点,请重新输入");

else

printf("猜对了");

}while(c!=b);

}

 

 

 

  1. 编写一个程序,求之值。

#include<stdio.h>

main(){

int a;

float sum1=0,sum2=0,sum;

for(a=1;a<=100;a++){

if(a%2==0)

sum1+=1/a;

else

sum2+=1/a;

}

sum=sum2-sum1;

printf("%f\n",sum);

}

 

 

 

 

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

#include<stdio.h>

main(){

int a,b=0,n,sum=0;

printf("请输入一个整数n\n");

scanf("%d",&n);

for(a=1;a<=n;a++){

sum=a+b+sum;

b+=a;

}

printf("%d",sum);

}

 

 

 

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

#include<stdio.h>

main(){

int a,b,c,n;

printf("请输入一个三位数\n");

scanf("%d",&n);

a=n%10;

b=n/10%10;

c=n/100;

printf("%d%d%d",a,b,c);

}

 

 

 

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

 

#include<stdio.h>

main()

{

char str[20];

int i=0,cnt=0;

gets(str);

puts(str);

while(str[i++]!='\0')

{

if(str[i]>='A'&&str[i]<='Z')

cnt++;

else

cnt+=0;

}

printf("大写字母的个数为:%d\n",cnt);

}

 

 

 

posted @ 2021-11-25 22:51  浅殇之城  阅读(16)  评论(0)    收藏  举报