第九次作业

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.

编写一个程序,求1-二分之一+三分之一-四分之一..........-99分之一+100分之一

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

 

 

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

#include <stdio.h>
main()
{
    int s=0,n,b=0,a;
    printf("请输入一个整数");
    scanf("%d",&n);
    for(a=1;a<=n;a++)
    {
        s+=a;
        b+=s;
    }
    printf("%d",b);

}

 

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

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

 

 

 

00匹马驮100担货,大马一匹驮3担,中马一匹驮2担,小马两匹驮1担。试编写程序计算大、中、小马的数目。(东师大,06年)

posted @ 2021-11-15 21:24  张云月  阅读(28)  评论(0编辑  收藏  举报