MrSix


私信TA

用户名:1658530280

访问量:154068

签 名:

等  级
排  名 16
经  验 21035
参赛次数 0
文章发表 23
年  龄 0
在职情况 学生
学  校 大连交通大学
专  业

  自我简介:

解题基础:


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分

54 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区

#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;
}
这个哪错了
2019-04-11 18:59:15
#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
2019-04-01 14:42:22
t为int,但是如果t是一个浮点数不就不准确了吗,所以t应该是double吧?
2019-03-07 16:57:50
#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
2019-02-25 22:21:57
#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
2019-02-25 22:21:40
#include<stdio.h>
int main()
{

	int i,t;
	scanf("%d",&i);
	if(i<=100000)
	{
		t=i*0.1;
	}
	else if(i>100000 && i<=200000)
	{
		t=100000*0.1+(i-100000)*0.075;
	}
	else if(i>200000 && i<=400000)
	{
		t=100000*0.1+100000*0.075+(i-200000)*0.05;
	}
	else if(i>400000 && i<=600000)
	{
		t=100000*0.1+100000*0.075+200000*0.05+(i-400000)*0.03;
	}
	else if(i>600000 && i<=1000000)
	{
		t=100000*0.1+100000*0.075+200000*0.05+200000*0.03+(i-600000)*0.015;
	}
	else if (i>1000000)
	{
		t=100000*0.1+100000*0.075+200000*0.05+200000*0.03+400000*0.015+(i-1000000)*0.01;
	}
	printf("%d",t);
	return 0;
}

这个哪里错了
2019-01-21 19:02:40
#include <stdio.h>
#include <stdlib.h>
int a,b;//a存利润,b算钱 
main()
{
 	scanf("%d",&a);
 	if(a<=100000)
 		b=0.1*a;
 	else if(a<=200000&&a>100000)//&&a>100000
 		b=10000+(a-100000)*0.075;
 	else if(a<=400000&&a>200000)
 		b=10000+100000*0.075+(a-200000)*0.05;
	else if(a<=600000&&a>400000)
 		b=10000+100000*0.075+200000*0.05+(a-400000)*0.03;
	else if(a<=1000000&&a>600000)
 		b=10000+100000*0.075+200000*0.05+200000*0.03+(a-600000)*0.015;
 	else if(a>1000000)
 		b=10000+100000*0.075+200000*0.05+200000*0.03+400000*0.015+(a-1000000)*0.01;
		 printf("%d",b);	
}
2019-01-21 17:50:03
#include<stdio.h>
int main()
{
    int x,y;
	scanf("%d",&x);
    if(x<=100000)
		y=x/10;
	else if(x>100000 && x<=200000)
        y=100000*0.1+(x-100000)*0.075;
	else if(x>200000 && x<=400000)
		y=100000*0.1+100000*0.075+(x-200000)*0.05;
	else if(x>400000 && x<=600000)
		y=100000*0.1+100000*0.075+200000*0.05+(x-400000)*0.03;
	else if(x>600000 && x<=1000000)
		y=100000*0.1+100000*0.075+200000*0.05+200000*0.03;
	else
		y=100000*0.1+100000*0.075+200000*0.05+200000*0.03+400000*0.015+(x-1000000)*0.01;
		printf("%d\n,"y);
	return 0;
}


这个哪里不对了呢
2018-12-02 16:02:18