如何在商场购物?(语言-c++)

在商场购物,一个作业本的单价是1元每本。如果购买10本以上,超过10本的作业本,每本0.8元。如果购买超过50本,超过50本的作业本,每本0.6元。
输入
输入包含一个整数n(0 < n <= 100),表示购买作业本的数目。
输出
输出一行,表示购买n本作业本的总费用。
样例输入
100
样例输出
72

以下是用C++实现的代码:


#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;

    double total_price;
    if (n >= 50) {
        total_price = n * 0.6;
    } else if (n >= 10 && n < 50) {
        total_price = n * 0.8;
    } else {
        total_price = n * 1.0;
    }

    cout << total_price << endl;

    return 0;
}



主要考察C++里 的if语句。
(不确定输出格式)


#include <iostream>
using namespace std;

int main() {
    int n;
    cin>>n;
    double res=0;
    if(n<=10) {//如果购买数量<=10
        res=n;//作业本价格为1元
    }else if(n<=50){//数量在10和50之间
        res=10+(n-10)*0.8;//前10本为1元一本,超过10本的部分为0.8
    }else{//数量大于50
        res=10+40*0.8+(n-50)*0.6;//前10本为1元1本,超过10本且不超过50本的部分为0.8,超过50本的部分为0.6
    }

    cout<<res;  
    return 0;
}

img

供参考:

#include <stdio.h>
int main()
{
    int    n;
    double total;
    scanf("%d", &n);
    if (n <= 10)
        total = n * 1.0;
    else if (n > 10 && n <= 50)
        total = (n - 10) * 0.8 + 10 * 1.0;
    else if (n > 50)
        total = (n - 50) * 0.6 + 40 * 0.8 + 10 * 1.0;
    printf("%.0f", total);
    return 0;
}