解题基础:
scanf("输入模式",地址列表);//输入语句及其格式。注:地址列表一定要加取地址符号&,如:&a,&b
printf("输出模式",输出列表);//输出语句及其格式。注:输出模式和输出列表一一对应,如:printf("%d%d",a,b);
switch(表达式){
case 常量值1:
若干语句1
break;//可省略
case 常量值2:
若干语句2
break;//可省略
......
case 常量值n:
若干语句n
break;//可省略
default://可省略
若干语句
}//开关语句及其格式,若表达式的值等于某个常量值,则进行某常量值相对应的语句,若没遇到break,则接着运行下一个常量值后面的语句,直到遇到break为止,若表达式的值不等于某个常量值,则执行default后面的若干语句,default可省略(不执行语句)
思路:因为题目要求,需要一个输入的值和一个输出的值,因为定义a的是一个整型变量int,所以a/100000的值也是一个int型变量(整数)(注:C语言的取整不是四舍五入,是直接舍去小数),直接用switch函数可以写出运算语句(当然if也行,不过比较麻烦),t=对应区间的全部数*相应利润+(a-区间最大数)*超出相应的利润
答案:
#include<stdio.h>
int main(){
int a,t;
scanf("%d",&a);
switch(a/100000){
case 0:
t=a*0.1;
break;
case 1:
t=100000*0.1+(a-100000)*0.075;
break;
case 2:
case 3:
t=100000*0.1+100000*0.075+(a-200000)*0.05;
break;
case 4:
case 5:
t=100000*0.1+100000*0.075+200000*0.05+(a-400000)*0.03;
case 6:
case 7:
case 8:
case 9:
t=100000*0.1+100000*0.075+200000*0.05+200000*0.03+(a-600000)*0.015;
default:
t=100000*0.1+100000*0.075+200000*0.05+200000*0.03+400000*0.015+(a-1000000)*0.01;
}
printf("%d",t);
return 0;
}ps:当a/100000=2时,因为常量值后面的若干语句中没有break,则接着运行下一个常量值(case 3)后面的若干语句,直到遇到break为止
同理,后面的4,6,7,8都是一样的
程序结束return 0;不可省。
因为题目表达原因,生活中t应该为浮点型常量,保留两位小数,可是答案不给对,所以代码就以答案为准
若对switch函数还有疑问,可以对应我的上一个题解题解1008:C语言程序设计教程(第三版)课后习题5.6 (C语言描述)参照学习
0.0分
47 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
@Saki printf("%d",b);#include<stdio.h> int main() { int a,b; scanf("%d",&a); switch(a/100000){ case 0: b=a*0.1;break; case 1: b=100000*0.1+(a-100000)*0.075;break; case 2: case 3: b=100000*0.1+100000*0.075+(a-200000)*0.05;break; case 4: case 5: b=100000*0.1+100000*0.075+200000*0.05+(a-400000)*0.03;break; case 6: case 7: case 8: case 9: b=100000*0.1+100000*0.075+200000*0.05+400000*0.03+(a-600000)*0.015; default: b=100000*0.1+100000*0.075+200000*0.05+400000*0.03+600000*0.015+(a-1000000)*0.001; } peintf("%d",b); return 0; } 这个哪错了#include<stdio.h> int main() { int amount, profit; scanf("%d",&amount); profit = 0; if(amount > 1000000){ profit = (amount -1000000)*0.01; amount -= amount - 1000000; } if(amount > 600000){ profit += (amount - 600000) * 0.015; amount -= amount - 600000; } if(amount > 400000){ profit += (amount - 400000) * 0.03; amount -= amount-400000; } if(amount > 200000){ profit += (amount - 200000 ) * 0.05; amount -= amount-200000; } if(amount > 100000){ profit += (amount - 100000 ) * 0.75; amount -= amount-100000; } profit += amount * 0.1; printf("%d\n", profit); re#include <stdio.h> int main() { int profit=0; int bonus,b1=0,b2=0,b3=0,b4=0,b5=0,b6=0; scanf("%d",&profit); if(profit>1000000) { b6=(profit-1000000)*0.01; profit = 1000000; }; if(profit<=1000000 && profit>600000){ b5=(profit-600000)*0.015; profit = 600000; }; if(profit<=600000 && profit>400000){ b4=(profit-400000)*0.03; profit=400000; }; if(profit<=400000 && profit>200000){ b3=(profit-200000)*0.05; profit=200000; }; if(profit<=200000 && profit>100000){ b2=(profit-100000)*0.075; profit=100000; }; if(profit<=100000){ b#include <stdio.h> int main() { int profit=0; int bonus,b1=0,b2=0,b3=0,b4=0,b5=0,b6=0; scanf("%d",&profit); if(profit>1000000) { b6=(profit-1000000)*0.01; profit = 1000000; }; if(profit<=1000000 && profit>600000){ b5=(profit-600000)*0.015; profit = 600000; }; if(profit<=600000 && profit>400000){ b4=(profit-400000)*0.03; profit=400000; }; if(profit<=400000 && profit>200000){ b3=(profit-200000)*0.05; profit=200000; }; if(profit<=200000 && profit>100000){ b2=(profit-100000)*0.075; profit=100000; }; if(profit<=100000){ b