第九次作业

1.猜数字,如果大了提示大了点,小了提示小了点

#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()
{
    double a,b=1;
    double c=0.0;
    for (a=1;a<=100;a++)
    {
        c+=b/a;
        b=-b;
        
    }
    printf("%.2lf\n",c);
}

  

 

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

#include <stdio.h>
int main()
{
    int sum1=0,i,n,sum2=0;
    printf("请输入一个正整数:");
    scanf("%d",&n);
    for (i=1;i<=n;i++)
    {
        sum1+=i;
        sum2+=sum1;
    }
    printf("他们的和为%d",sum2);
    return 0;
}

  

 

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

#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);
}

  

 

 

5.100匹马驮100担货,大马一匹驮3担,中马一匹驮2担,小马两匹驮1担。试编写程序计算大、中、小马的数目。
#include <stdio.h>
int main()
{
    int a,b,c=0,i;//a为大马,b为中马,c为小马
    for (a=0;a<33;a++)
    {
        for (b=0;b<50;b++)
        {
            c=100-a-b;
            if(c%2!=0)
            {
            continue;
            }
            i=3*a+2*b+c/2;
            if (i==100)
            printf("大马有%d只,中马有%d只,小马有%d只\n",a,b,c);
        }
    }
    return 0;
}

  

 

posted @ 2021-11-15 23:38  苏本琪  阅读(30)  评论(0)    收藏  举报