问题标题: 酷町堂:4454

0
0
已解决
王俊杰
王俊杰
高级光能
高级光能

4454   酷町猫买鱼经验值:0

题目描述 Description

酷町猫去菜场买鱼,发现随着要买的鱼的重量(x)不同,单价也会变化,现在将价格总结如下:
如果x>5,每斤售价4元;
如果每斤售价3<x<=5,每斤5元;
否则, 每斤6元。
现在让同学们帮忙算一下,应付钱数y是多少,四舍五入保留一位小数。(1<=x<=10,x可能是小数)

输入描述 Input Description

一个实数x,表示买鱼的重量

输出描述 Output Description

买鱼的花费,四舍五入保留一位小数

样例输入 Sample Input

6.53

样例输出 Sample Output

26.1

数据范围及提示 Data Size & Hint

1<=x<=10

40分代码:

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    double x;
    cin>>x;
    if(x>5){
        printf("%.1f",x*4);
    }
    if(x<=5){
        printf("%.1f",x*5);
    }
    else{
        printf("%.1f",x*6);
    }
    
    return 0;

why


0
0
王俊杰
王俊杰
高级光能
高级光能

曹砚青@

80分:

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    double x,y;
    cin>>x;
    if(x>5){
        y = x * 4;
    }
    if(x>=3 && x<=5)
        y = x * 5;
    else{
        y = x * 6;
    }
    printf("%.1f",(int)(10*y+0.5)/10.0);
    return 0;

why

0
0
曹砚青
曹砚青
中级光能
中级光能

这个可以看作是一个分段:

1、if (x>5) y = x * 4

2、if (x>=3 && x<=5) y = x * 5

3、else y = x * 6

4、四舍五入:printf("%.1f",(int)(10*y+0.5)/10.0);

我要回答