第九次作业

1.猜数字,随机产生一个0-99的数字,猜猜看如果大了,就提示大了点,小了就提示小了点,直到猜对为止。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
    int a, b ,c=0;
    srand((unsigned)time(NULL));
    a = rand();
    b = a % 100;
    while (b != c)
    {
        printf("请输入一个0--99的数字:");
        scanf_s("%d", &c);
        if (b < c)
            printf("大了点!\n");
        if (b > c)
            printf("小了点!\n");
    }
    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>
main()
{
    int a, b=0, sum=0,n;
    printf("请输入n的取值:");
    scanf_s("%d",&n);
    for (a = 1; a <=n; a++)
    {
        b += a;
        sum += b;
    }
    printf("他们的和为%d",sum);
}

4.编写程序,将一个数倒过来后输出。

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

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

 

posted @ 2021-11-19 21:26  鹏宇0240  阅读(29)  评论(0)    收藏  举报