谭浩强C语言书5.8(2)题

来源:互联网 发布:申请域名多少钱 编辑:程序博客网 时间:2024/04/30 00:52

题意见代码~

然后这道题主要考switch怎么映射一个范围,我先应用第八章的函数知识来把范围映射成一个数,然后解决问题。

//problem 5.8#include <stdio.h>int f(int x){    if(x <= 100000) return 1;    if(x <= 200000) return 2;    if(x <= 400000) return 3;    if(x <= 600000) return 4;    if(x <= 1000000) return 5;    return 6;}void main(){    float x , y;    scanf("%f",&x);    switch (f(x))    {        case 1 : y = x * 0.1;        break;        case 2 : y = 100000 * (0.1) + (x - 100000) * 0.075;        break;        case 3 : y = 100000 * (0.1 + 0.075) + (x - 200000) * 0.05;        break;        case 4 : y = 100000 * (0.1 + 0.075 + 2*0.05) + (x-400000) * 0.03;        break;        case 5 : y = 100000 * (0.1 + 0.075 + 2*0.05 + 2*0.03) + (x-600000) * 0.015;        break;        default : y = 100000 * (0.1 + 0.075 + 2*0.05 + 2*0.03 + 4*0.015) + (x-1000000) * 0.001;    }    printf("%f\n" , y);}


0 0
原创粉丝点击