2021.11.9 第八章综合练习题《C语言设计精髓》

2021.11.9 第八章综合练习题《C语言设计精髓》,第1张

2021.11.9 第八章综合练习题《C语言设计精髓》

1

三天打渔两天晒网(4分)

题目内容:

中国有句俗语叫“三天打鱼两天晒网”,某人从1990年1月1日起开始“三天打鱼两天晒网”,即工作三天,然后再休息两天。问这个人在以后的某一天中是在工作还是在休息。从键盘任意输入一天,编程判断他是在工作还是在休息,如果是在工作,则输出:He is working,如果是在休息,则输出:He is having a rest,如果输入的年份小于1990或者输入的月份和日期不合法,则输出:Invalid input。

逻辑:

1.首先对输入数据进行检测,检测其合格与否,注意区分闰年

2.计算当前日期与对应的日期过的总天数

3.检测是否在工作

#include
#include
int main()
{
    int year,month,day,sum,count;
    int a[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
    scanf("%4d-%2d-%2d",&year,&month,&day);
    if(year<1990||month>12||month<0)
    {
        printf("Invalid input");
    }
    else if(day>a[month]&&month!=2)
        printf("Invalid input");
    else if(month==2)
        if((year%4==0)&&(year%100!=0)||(year%400==0))
        {
            if(day>29)
                printf("Invalid input");
        }
        else
        {
            if(day>28)
                printf("Invalid input");
        }


    else
    {
        sum=0;
    for (int i=0;i<(year-1990);i++)
    {
        if((year%4==0)&&(year%100!=0)||(year%400==0))
            sum=sum+366;
        else
            sum=sum+365;
    }
    if ((year%4==0)&&(year%100!=0)||(year%400==0))
    {
         for(int j=1;j         {
            sum=sum+a[1][j];
         }
    }
    else
        for(int j=1;j         {
            sum=sum+a[1][j];
         }

    sum=sum+day;
    count=sum%5;
    if(count==4||count==0)
        printf("He is having a rest");
    else
        printf("He is working");
    }

    return 0;
}
 

2

统计用户输入(4分)

题目内容:

从键盘读取用户输入直到遇到#字符,编写程序统计读取的空格数目、读取的换行符数目以及读取的所有其他字符数目。(要求用getchar()输入字符)

程序运行结果示例1:

#include
#include
int main()
{
    int space,newline,others;
    char ch;
    space=0;
    newline=0;
    others=0;
    printf("Please input a string end by #:");
    while(1)
    {
        scanf("%c",&ch);
        if(ch=='#')
            break;
        if(ch==' ')
            space++;
        if(ch=='n')
            newline++;
        else
            others++;
    }
    printf("space: %d,newline: %d,others: %dn",space,newline,others);
    return 0;
}
 

3

统计正整数中指定数字的个数(4分)

题目内容:

从键盘输入一个正整数number,求其中含有指定数字digit的个数。例如:从键盘输入正整数number=1222,若digit=2,则1223中含有 3个2,要求用函数实现。函数原型为:int CountDigit(int number,int digit);

#include
#include
int CountDigit(int number,int digit);

int CountDigit(int number,int digit)
{
    int a[10]={0},b;

    while (number!=0)
    {
        b=number%10;
        a[b]++;
        number=number/10;
    }
    printf("%dn",a[digit]);

}

int main()
{
    int number,digit,sum;
    printf("Input m,n:n");
    scanf("%d,%d",&number,&digit);
    CountDigit(number,digit);
    return 0;
}

4

玫瑰花数(4分)

题目内容:

如果一个n位正整数等于它的n个数字的n次方和,则称该数为n位自方幂数。四位自方幂数称为玫瑰花数。编程计算并输出所有的玫瑰花数。

#include
#include
int main()
{
    for(int number=1000;number<10000;number++)
    {
       int a[]={0},x;
       x=number;
       int i=0;
       while(x!=0)
       {
        a[i]=x%10;
        x=x/10;
        i++;
       }
       if(number=pow(a[0],4)+pow(a[1],4)+pow(a[2],4)+pow(a[3],4))
        {
           printf("%dt",number);
        }

    }
    return 0;
}
//这是我自己写的,但是实在不知道哪里出错了,回头想明白了回来修改

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5432720.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存