习题4-选择结构程序设计

1.代理费计算

#include <stdio.h>
int main() {
	float before;
	int n = 0;
	scanf_s("%f", &before);
	if (before > 0 && before < 3000)
		n = 1;
	else if (before >= 3000 && before <= 6000)
		n = 2;
	else if (before >= 6000 && before < 10000)
		n = 3;
	switch (n) {
	case 1:printf("the cost is %.2f RMB", before * 0.012 + 18); break;
	case 2:printf("the cost is %.2f RMB", before * 0.006 + 36); break;
	case 3:printf("the cost is %.2f RMB", before * 0.003 + 54); break;
	default:printf("data error!"); break;
	}
	return 0;
}

2.分段函数求解

#include <stdio.h>
#include <math.h>
int main() {
	float a, x, y;
	scanf_s("a=%f,x=%f", &a, &x);
	if (x == a || x == -a)
		y = 0;
	else if (x > -a && x < a)
		y = sqrt(pow(a, 2) - pow(x, 2));
	else
		y = x;
	printf("a=%.1f,x=%.1f,y=%.2f", a, x, y);
}

3.某点的建筑高度计算

#include <stdio.h>
#include <math.h>
int main() {
	float x, y;
	float d1, d2, d3, d4;
	int h;
	scanf_s("%f,%f", &x, &y);
	d1 = sqrt(pow(x - 2, 2) + pow(y - 2, 2));
	d2 = sqrt(pow(x - 2, 2) + pow(y + 2, 2));
	d3 = sqrt(pow(x + 2, 2) + pow(y - 2, 2));
	d4 = sqrt(pow(x + 2, 2) + pow(y + 2, 2));
	if (d1 <= 1 || d2 <= 1 || d3 <= 1 || d4 <= 1)
		h = 10;
	else
		h = 0;
	printf("height is %d", h);
	return 0;
}

4.百分制成绩转化为等级

#include <stdio.h>
int main() {
	int score, n = 0;
	scanf_s("%d", &score);
	if (score >= 0 && score < 60)
		n = 1;
	else if (score < 70)
		n = 2;
	else if (score < 80)
		n = 3;
	else if (score < 90)
		n = 4;
	else if (score <= 100)
		n = 5;
	switch (n) {
	case 1:printf("Level E"); break;
	case 2:printf("Level D"); break;
	case 3:printf("Level C"); break;
	case 4:printf("Level B"); break;
	case 5:printf("Level A"); break;
	default:printf("Input Error"); break;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值