中国慕课MOCC哈工大C语言第7周编程题在线测试

1
n层嵌套平方根的计算(4分)

题目内容:

编写程序利用递归法实现如下所示n层嵌套平方根的计算:

递归函数原型:double Y(double x, int n);

程序运行结果示例1:

Please input x and n:16,1↙

Result=4.00

程序运行结果示例2:

Please input x and n:16,2↙

Result=4.47

程序运行结果示例3:

Please input x and n:16,3↙

Result=4.52

程序运行结果示例4:

Please input x and n:16,0↙

Result=0.00

#include <stdio.h>
#include <math.h>
double Y(double x,int n);
int main()
{
	double x,Result;
	int n;
	printf("Please input x and n:");
	scanf("%lf,%d",&x,&n);
	Result=Y(x,n);
	printf("Result=%.2f\n",Result);
	return 0;
}
double Y(double x,int n)
{
	if(n==0 || n<0 || x<0)
	return 0;
	else
	return sqrt(x+Y(x,n-1));
}

2
递归法求和(4分)

题目内容:

用递归方法计算如下求和计算

sum = 1 + 2 + 3 + … + n

递归函数原型:int Sum(int n);

程序运行结果示例1:

Please input n:5↙

sum=15

程序运行结果示例2:

Please input n:0↙

data error!

程序运行结果示例3:

Please input n:-3↙

data error!

#include <stdio.h>
#include <math.h>
int Sum(int n);
int main()
{
	int n,sum;
	printf("Please input n:");
	scanf("%d",&n);
	if(n<=0)
	printf("data error!\n");
	else
	{
		sum=Sum(n);
		printf("sum=%d\n",sum);
	}
	return 0;
}
int Sum(int n)
{
	int i,a=0;
	for(i=1;i<=n;i++)
	a=a+i;
	return a;
}

3
猴子吃桃程序_扩展3(4分)

题目内容:

猴子第一天摘了若干个桃子,吃了一半,不过瘾,又多吃了1个。第二天早上将剩余的桃子又吃掉一半,并且又多吃了1个。此后每天都是吃掉前一天剩下的一半零一个。到第n天再想吃时,发现只剩下1个桃子,问第一天它摘了多少桃子?为了加强交互性,由用户输入天数n,即假设第n天的桃子数为1。

要求采用递归法求解。

递归函数原型:int Monkey(int n, int x);

函数功能:从第n天只剩下一个桃子反向逆推出第1天的桃子数

程序运行结果示例1:

Input days n:5↙

x=46

程序运行结果示例2:

Input days n:10↙

x=1534

#include <stdio.h>
#include <math.h>
int Monkey(int n, int x);
int main()
{
	int n,x,z;
	printf("Input days n:");
	scanf("%d",&n);
	z=Monkey(n,x);
	printf("x=%d\n",z);
	return 0;
}
int Monkey(int n, int x)
{
	int i,a=1;
	for(i=1;i<n;i++)
	{
		x=2*(a+1);
		a=x;
	}
	return a;
}

4
网购打折商品V2.0(5分)

题目内容:

某网上购物网站对用户实行优惠,买家购物货款p越多,则折扣越多。

标准如下:

p<100元 没有折扣

100元≤p<200元 5%折扣

200元≤p<500元 8%折扣

500元≤p<1000元 10%折扣

1000元≤p 15%折扣

【提示】:从题意可以看出,折扣的变化是有规律的。当购物金额达到“100元”的2倍、5倍、10倍时,折扣值就会发生变化。假如一个变量c代表100的倍数,则当c<1时,无折扣;当1≤c<2时,折扣d=5%;当2≤c<5时,折扣d=8%;当5≤c<10时,折扣d=10%;当10≤c时,折扣d=15%。

注:程序中与价格相关的数据类型为float

程序运行结果示例1:

Input payment:90↙

price = 90.0

程序运行结果示例2:

Input payment:100↙

price = 95.0

程序运行结果示例3:

Input payment:300↙

price = 276.0

程序运行结果示例4:

Input payment:1000↙

price = 850.0

#include <stdio.h>
#include <math.h>
int main()
{
	float x;
	printf("Input payment:");
	scanf("%f",&x);
	if(x<100)
		printf("price = %.1f\n",x);
	if(x>=100 && x<200)
		printf("price = %.1f\n",(1-0.05)*x);
	if(x>=200 && x<500)
		printf("price = %.1f\n",(1-0.08)*x);
	if(x>=500 && x<1000)
		printf("price = %.1f\n",(1-0.1)*x);
	if(x>=1000)
		printf("price = %.1f\n",(1-0.15)*x);
	return 0;
}

经测试,以上答案均有效1

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值