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

**

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

**
题目内容:

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

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

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

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb

#include <stdio.h>

int main()
{
	int man, woman, children, money;
	printf("Man   Women   Children\n");
	for (man = 0; man < 31; man++)
	{
		for (woman = 0; woman < 31; woman++)
		{
		    children = 30 - man - woman;
		    money = man * 3 + woman * 2 + children;
		    if (money == 50)
            {
                printf("%3d%8d%8d\n",man,woman,children);
            }

		}
	}
	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”

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb

#include <stdio.h>
#include<stdlib.h>
#include<math.h>
 
int same(int a[10]);//判断10个数是否相等,相互不相等返回1
int many(int a);//判断一个整型数是几位数
 
main()
{   
    int a[10] ,i ,j ,s ,l;  //a储存10个数字的数组,i年龄循环变量,j将数字存入数组,s储存四位数,l储存六位数
    for(i=10;i<22;i++){
        s=pow((double)i,3);
        l=pow((double)i,4);
        if(many(l)!=6)
            continue;//如果不是六位数则退出当次循环
        for(j=0;j<4;j++){
            a[j]=s%10;
            s=s/10;
        }//存入四位数
        for(j=4;j<10;j++){
            a[j]=l%10;
            l=l/10;
        }//存入六位数
        if(same(a))
            printf("age=%d\n",i);//输出
    }
    system("pause");
}
 
int many(int a){
    int i=0;
    while(a>0){
        a=a/10;
        i++;
    }
    return i;
}
 
int same(int a[10]){
    int i,j,flag=1;
    for(i=0;i<10;i++){
        for(j=i+1;j<10;j++){
            if(a[i]==a[j])
                flag=0;
        }
    }
    return flag;
}

**

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”

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb

#include <stdio.h>

int leapyear(int a);

int main()
{
    int born, now;
    int i, count = 0;
    printf("Input your birth year:");
    scanf("%d", &born);
    printf("Input this year:");
    getchar();
    scanf("%d", &now);
    for (i = born; i <= now; i++)
    {
        if (leapyear(i))
        {
            printf("%d\n", i);
            count += 1;
        }
    }
    printf("count=%d\n", count);
    return 0;
}


int leapyear(int a)
{
    int flag = 0;
    if (a % 100 == 0)
    {
        if (a % 400 == 0)
        {
            flag = 1;
        }
    }
    else
    {
        if (a % 4 == 0)
        {
            flag = 1;
        }
    }
    return flag;
}

**

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”

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb

#include <stdio.h>

int leapyear(int a);

int main()
{
    int born, now;
    int i;
    long unsigned int count = 0;
    printf("Input your birth year:");
    scanf("%d", &born);
    printf("Input this year:");
    getchar();
    scanf("%d", &now);
    for (i = born; i < now; i++)
    {
        if (leapyear(i))
        {
            count += 75*60*24*366;
        }
        else
        {
            count += 75*60*24*365;
        }
    }
    printf("The heart beats in your life: %lu", count);
    return 0;
}


int leapyear(int a)
{
    int flag = 0;
    if (a % 100 == 0)
    {
        if (a % 400 == 0)
        {
            flag = 1;
        }
    }
    else
    {
        if (a % 4 == 0)
        {
            flag = 1;
        }
    }
    return flag;
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

比巧克力巧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值