c语言程序设计精髓 第5周编程题在线测试

1马克思手稿中的趣味数学题(4分)

题目内容:

编程求解马克思手稿中的趣味数学题:有30个人,其中有男人、女人和小孩,在一家饭馆里吃饭共花了50先令,每个男人各花3先令,每个女人各花2先令,每个小孩各花1先令,请编程计算男人、女人和小孩各有几人?

输出提示信息: “Man Women Children\n” (注意:每个单词之间有3个空格)

输出格式:“%3d%8d%8d\n” (注意:输出数据按照男人的数量递增的顺序给出)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
  int main()
  {
    int num=30,money= 50,x,y,z;
    printf("Man   Women   Children\n");
    for(x=0;x<17;x++){
        for(y=0;y<25;y++){
            z = 30 -x -y;
            if(z>=0 && 3*x+2*y+z==50){
                printf("%3d%8d%8d\n",x,y,z);
            }
        }
    }

    return 0;
  }

2猜神童年龄(4分)

题目内容:

美国数学家维纳(N.Wiener)智力早熟,11岁就上了大学。他曾在1935~1936年应邀来中国清华大学讲学。一次,他参加某个重要会议,年轻的脸孔引人注目。于是有人询问他的年龄,他回答说:“我年龄的立方是一个4位数。我年龄的4次方是一个6位数。这

10个数字正好包含了从0到9这10个数字,每个都恰好出现1次。”请你编程算出他当时到底有多年轻。

【解题思路】:因为已知年龄的立方是一个4位数字,所以可以推断年龄的范围在10到22之间,因此确定穷举范围为10到22。如果年龄还满足“年龄的4次方是一个6位数”这个条件,则先计算年龄的立方值的每一位数字,从低位到高位分别保存到变量b1,b2,b3,b4

中,再计算年龄的4次方值的每一位数字,从低位到高位分别保存到变量a1,a2,a3,a4,a5,a6中。如果上述10个数字互不相同,则必定是包含了从0到9这10个数字并且每个都恰好出现1次,因此只要判断上述10个数字互不相同,即可确定这个年龄值为所求。

输出格式:“age=%d\n”

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
  int main()
  {
    int b1,b2,b3,b4,a1,a2,a3,a4,a5,a6,c, a [10] ={0};
    for(int x=10;x<=22;x++){

        int s3 = x*x*x;
        int s4 = x*s3;
        if(s4 > 100000 && s4 < 1000000){
            int flag =1;
            b1 = s3%10;
            b2 = s3/10%10;
            b3 = s3/100%10;
            b4 = s3/1000%10;
            a1 =s4%10;
            a2 =s4/10%10;
            a3 =s4/100%10;
            a4 =s4/1000%10;
            a5 =s4/10000%10;
            a6 =s4/100000%10;

            a[b1]=1;
            a[b2]=1;
            a[b3]=1;
            a[b4]=1;
            a[a1]=1;
            a[a2]=1;
            a[a3]=1;
            a[a4]=1;
            a[a5]=1;
            a[a6]=1;

            for(int j=0;j<10;j++){
                if(a[j] !=1){
                  flag = 0;
                  break;
                }
            }
            if(flag){
                printf("age=%d\n",x);
            }
            for(int j=0;j<10;j++){
                a[j] =0;
            }
        }
    }

    return 0;
  }

3闰年相关的问题v3.0——计算有多少闰年(4分)

题目内容:

从键盘输入你的出生年和今年的年份,编程判断并输出从你的出生年到今年之间中有多少个闰年。

程序的运行结果示例1:

Input your birth year:2000↙

Input this year:2020↙

2000

2004

2008

2012

2016

2020

count=6

输入提示信息:“Input your birth year:”

输入提示信息:“Input this year:”

输入格式:“%d”

输出格式:

闰年年份: “%d\n”

闰年总数:“count=%d\n”

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
  int main()
  {
      int year,year1,count=0;
      printf("Input your birth year:");
      scanf("%d",&year);
      printf("Input this year:");
      scanf("%d",&year1);
      for(;year<=year1;year++){
        if(year%4==0&& year % 100 !=0 || year % 400 ==0){
            count++;
            printf("%d\n",year);
        }
      }
     printf("count=%d\n",count);

    return 0;
  }

4闰年相关的问题v4.0——计算心跳数(4分)

题目内容:

假设人的心率为每分钟跳75下,编程从键盘输入你的出生年和今年的年份,然后以年为单位计算并输出从你出生开始到目前为止的生命中已有的心跳总数(要求考虑闰年)。

程序运行结果示例1:

Input your birth year:1986↙

Input this year:2016↙

The heart beats in your life: 1183356000

输入提示信息:“Input your birth year:”

输入提示信息:“Input this year:”

输入格式:“%d”

输出格式:“The heart beats in your life: %lu”

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
  int main()
  {
      int year,year1,year2,count=0;
      unsigned long bitss=0;
      printf("Input your birth year:");
      scanf("%d",&year2);
      printf("Input this year:");
      scanf("%d",&year1);
      year=year2;
      for(;year<year1;year++){
        if(year%4==0&& year % 100 !=0 || year % 400 ==0){
            count++;
            //printf("%d\n",year);
        }
      }
     
     bitss = ((year1-year2)*365+count)*24*60*75;
     printf("The heart beats in your life: %lu",bitss);

    return 0;
  }
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值