仅代表个人看法
如有侵权请联系
题目8:给出一百分制成绩,要求输出成绩等级'A'、'B'、'C'、'D'、'E'。90 分以上为'A',80~89分为'B' ,70~70分为'C' ,60~69分为'D' ,60分以下为'E'。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
float score;
char grade;
printf("请输入学生成绩:");
scanf("%f", &score);
while (score > 100 || score < 0)
{
printf("\n输入有误,请重输");
scanf("%f", &score);
}
switch ((int)(score / 10))
{
case 10:
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
grade = 'E';
}
printf("成绩是%5.1f,相应的等级是%c\n", score, grade);
return 0;
}
结果如图:
题目9:给一个不多于5位的正整数,要求:
①求出它是几位数;
②分别输出每一位数字;
③按逆序输出各位数字,例如原数为321,应输出123。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int num, indiv, ten, hundred, thousand, ten_thousand, place;
printf("请输入一个整数(0~99999):");
scanf("%d", &num);
if (num > 9999)
place = 5;
else if (num > 999)
place = 4;
else if (num > 99)
place = 3;
else if (num > 9)
place = 2;
else
place = 1;
printf("位数:%d\n", place);
printf("毎位数字为:");
ten_thousand = num / 10000;
thousand = (int)(num - ten_thousand * 10000) / 1000;
hundred = (int)(num - ten_thousand * 10000 - thousand * 1000) / 100;
ten = (int)(num - ten_thousand * 10000 - thousand * 1000 - hundred * 100) / 10;
indiv = (int)(num - ten_thousand * 10000 - thousand * 1000 - hundred * 100 - ten * 10);
switch (place)
{
case 5:
printf("%d,%d,%d,%d,%d", ten_thousand, thousand, hundred, ten, indiv);
printf("\n反序数字为:");
printf("%d%d%d%d%d\n", indiv, ten, hundred, thousand, ten_thousand);
break;
case 4:
printf("%d,%d,%d,%d", thousand, hundred, ten, indiv);
printf("\n反序数字为:");
printf("%d%d%d%d\n", indiv, ten, hundred, thousand);
break;
case 3:
printf("%d,%d,%d", hundred, ten, indiv);
printf("\n反序数字为:");
printf(" %d% d% d(n", indiv, ten, hundred);
break;
case 2:
printf("%d,%d", ten, indiv);
printf("\n反序数字为:");
printf("%d%d\n", indiv, ten);
break;
case 1:
printf("%d", indiv);
printf("\n反序数字为:");
printf("%dn", indiv);
break;
}
return 0;
}
运行结果:
题目10:
企业发放的奖金根据利润提成。
利润 I 低于或等于 100 000 元的,奖金可提成10%;
利润高于100 000 元,低于200 000元(100 000<I≤200 000)时,低于100 000元的部分按10%提成,高于 100 000元的部分,可提成 7.5%
200 000<I≤400 000 时,低于200 000元的部分仍按上述办法提成(下同)。
高于200 000 元的部分按5%提成;400 000<I≤600 000元时,高于 400 000元的部分按 3%提成;
600 000<I≤1 000 000 时,高于600 000元的部分按1.5%提成
I >1 000 000时,超过1 000 000元的部分按1%提成。从键盘输入当月利润 I,求应发奖金总数。
要求:
(1)用if语句编程序;
(2)用 switch 语句编程序。
1、
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int i;
double bonus, bon1, bon2, bon4, bon6, bon10;
bon1 = 100000 * 0.1;
bon2 = bon1 + 100000 * 0.075;
bon4 = bon2 + 100000 * 0.05;
bon6 = bon4 + 100000 * 0.03;
bon10 = bon6 + 400000 * 0.015;
printf("请输入利润i:");
scanf("%d", &i);
if (i <= 100000)
bonus = i * 0.1;
else if (i <= 200000)
bonus = bon1 + (i - 100000) * 0.075;
else if (i <= 400000)
bonus = bon2 + (i - 200000) * 0.05;
else if (i <= 600000)
bonus = bon4 + (i - 400000) * 0.03;
else if (i <= 1000000)
bonus = bon6 + (i - 600000) * 0.015;
else
bonus = bon10 + (i - 1000000) * 0.01;
printf("奖金是∶%10.2f\n", bonus);
return 0;
}
结果:
分析
此题的关键在于正确写出每一区间的奖金计算公式。例如利润在 100 000~200 000元时,奖金应由两部分组成∶
①利润为 100 000元时应得的奖金,即 100 000元×0.1。
②100 000元以上部分应得的奖金,即(num一100 000)×0.075元。
同理,200 000~400 000 元这个区间的奖金也应由两部分组成:
①利润为 200 000元时应得的奖金,即 100 000×0.1+100 000×0.075。
②200 000元以上部分应得的奖金,即(num—200 000)×0.05元。
先把100 000元、200 000元、400 000元、600 000元、1 000 000元各关键点的奖金计算出来,即 bonl1,bon2,bon4,bon6 和 bonl0。然后再加上各区间附加部分的奖金即可。
2、
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int i;
double bonus, bon1, bon2, bon4, bon6, bon10;
int branch;
bon1 = 100000 * 0.1;
bon2 = bon1 + 100000 * 0.075;
bon4 = bon2 + 200000 * 0.05;
bon6 = bon4 + 200000 * 0.03;
bon10 = bon6 + 400000 * 0.015;
printf("请输入利润 i∶");
scanf("%d", &i);
branch = i / 100000;
if (branch > 10)
branch = 10;
switch (branch)
{
case 0:
bonus = i * 0.1;
break;
case 1:
bonus = bon1 + (i - 100000) * 0.075;
break;
case 2:
case 3:
bonus = bon2 + (i - 200000) * 0.05;
break;
case 4:
case 5:
bonus = bon4 + (i - 400000) * 0.03;
break;
case 6:
case 7:
case 8:
case 9:
bonus = bon6 + (i - 600000) * 0.015;
break;
case 10:
bonus = bon10 + (i - 1000000) * 0.01;
}
printf("奖金是 %10.2f\n", bonus);
return 0;
}
结果
此方法暂不做具体讲解,如有不会请留言
题目11:输入 4个整数,要求按由小到大的顺序输出
解∶此题采用依次比较的方法排出其大小顺序。在学习了循环和数组以后,可以掌握更多的排序方法。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int t, a, b, c, d;
printf("请输入4个数∶");
scanf("%d,%d,%d,%d", &a, &b, &c, &d);
printf("a=%d,b=%d,c=%d,d=%d\n", a, b, c, d);
if (a > b)
{
t = a;
a = b;
b = t;
}
if (a > c)
{
t = a;
a = c;
c = t;
}
if (a > d)
{
t = a;
a = d;
d = t;
}
if (b > c)
{
t = b;
b = c;
c = t;
}
if (b > d)
{
t = b;
b = d;
d = t;
}
if (c > d)
{
t = c;
c = d;
d = t;
}
printf("排序结果如下∶\n");
printf("%d %d %d %d \n", a, b, c, d);
return 0;
}
结果:
(如果排序方式为由大到小,则倒数第3行改为 printf("%d %d %d %d \n", d,c,b,a);
)
题目12:有4个圆塔,圆心分别为(2,2)、(一2,2)、(—2,一2)、(2,一2),圆半径为1,见图 4.5。这 4个塔的高度为10m,塔以外无建筑物。今输入任一点的坐标,求该点的建筑高度(塔外的高度为零)。
示意图为下(忘拿尺子了,各位将就看吧)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int h = 10;
float x1 = 2, y1 = 2, x2 = -2, y2 = 2, x3 = -2, y3 = -2, x4 = 2, y4 = -2, x, y, d1, d2, d3, d4;
printf("请输入一个点(x,y)∶");
scanf("%f,%f", &x, &y);
d1 = (x - x4) * (x - x4) + (y - y4) * (y - y4);
d2 = (x - x1) * (x - x1) + (y - y1) * (y - y1);
d3 = (x - x2) * (x - x2) + (y - y2) * (y - y2);
d4 = (x - x3) * (x - x3) + (y - y3) * (y - y3);
if (d1 > 1 && d2 > 1 && d3 > 1 && d4 > 1)
h = 0;
printf("该点高度为%d\n", h);
return 0;
}
运行结果:
本文遵守协议
如有转载表明出处
谢谢点赞与浏览