中国大学MOOC哈工大C语言程序设计第5周编程题在线测试

中国大学MOOC哈工大C语言程序设计第5周编程题在线测试


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

题目内容:

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

输出提示信息: “Man Women Children\n” (注意:每个单词之间有3个空格)
输出格式:"%3d%8d%8d\n" (注意:输出数据按照男人的数量递增的顺序给出)

#include <stdio.h>
int main()
{
    printf("Man   Women   Children\n");
    int m,w,c;
    for( m=0;m<30;m++)
        for (w=0; w<30; w++)
            for (c=0; c<30; c++)
        if((m+w+c)==30&&(m*3+w*2+c*1)==50){
            printf("%3d%8d%8d\n",m,w,c);
    }
}

程序的运行结果示例1:
Man Women Children
0 20 10
1 18 11
2 16 12
3 14 13
4 12 14
5 10 15
6 8 16
7 6 17
8 4 18
9 2 19
10 0 20

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>
void divide(long n,int a[],int low,int high)//从低位到高位分离
{
    for(;low <= high;low++){
        a[low] = n%10;
        n = n/10;
    }
}

int judge(int a[],int n)//判断数组各元素,不同返回1,否则返回0
{
    int i;
    int b[n] ;
    for(i = 0;i < n;i++)
        b[i] = 0;
    for(i = 0;i < 10;i++){
        b[a[i]]++;
    }
    for(i = 0;i < 10;i++)
    {
        if(b[i]!=1)return 0;
    }
    return 1;
}
int main()
{
    long x,y;
    int a[10],n;
    int i = 0;
    for(n = 10;n <= 21;n++)
    {
        x = n*n*n;
        y = x*n;
        divide(x,a,0,3);
        divide(y,a,4,9);
        if(judge(a,10))printf("age=%d\n",n);
    }
    return 0;
}

程序的运行结果示例1:
age=18

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

题目内容:

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

输入提示信息:“Input your birth year:”
输入提示信息:“Input this year:”
输入格式:"%d"
输出格式:
闰年年份: “%d\n”
闰年总数:“count=%d\n”

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

程序的运行结果示例1:
Input your birth year:2000↙
Input this year:2020↙
2000
2004
2008
2012
2016
2020
count=6

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

题目内容:

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

输入提示信息:“Input your birth year:”
输入提示信息:“Input this year:”
输入格式:"%d"
输出格式:“The heart beats in your life: %lu”

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

程序运行结果示例1:
Input your birth year:1986↙
Input this year:2016↙
The heart beats in your life: 1183356000

第6周编程题:中国大学MOOC哈工大C语言程序设计第6周编程题在线测试

  • 5
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值