首页 > 解决方案 > 我提出什么条件才能进一步增加 5% 的折扣?

问题描述

要支付的金额通常是通过将单价乘以销售数量来计算的。但是,以下适用:

#include <iostream>
using namespace std;
int main(){
    int items;
    int i=0;
    int item_num;
    double unit_price;
    double price_break;
    double quantity_sold;
    double amount_paid;
    double discount=0.00;
    double net_payment;

    amount_paid=unit_price*quantity_sold;
    if(quantity_sold>=price_break){
        discount=0.1*amount_paid;
        new_amount=amount_paid-discount
        //Quantity sold is more than price break which places discount at 10%
        net_payment=new_amount;         
    }else{
        //Need help with this. How do I further add 5%?
        if((quantity_sold>=price_break)&&(new_amount>1000)){
            discount=0.1*amount_paid;
            net_payment=amount_paid-discount;
        }
        else{
            if(amount_paid>1000){
                // discount is 5% since the amount to be paid exceeds $1000
                discount=0.05*amount_paid;
                net_payment=amount_paid-discount;
            }
            else{
                if(quantity_sold<price_break){
                    //No discount and the amount to be paid doesn't exceeds $1000
                    net_payment=amount_paid;
                }
            }
        }
    }

标签: c++

解决方案


由于第二个和第三个条件导致同样的事情发生,您可以将规则简化如下:

  1. 首先,如果售出的数量大于或等于价格中断数量,则对该商品给予 10% 的折扣。

  2. 无论在第 1 步中是否给予折扣,如果要支付的金额超过 1,000 美元,则给予 5% 的折扣。

因此,只需摆脱else外部的块,if而是只需测试金额是否超过 1,000 美元。

amount_paid=unit_price*quantity_sold;
if(quantity_sold>=price_break){
// apply first discount if quantity is high enough
    discount=0.1*amount_paid;
    amount_paid-=discount;
}
if(amount_paid>1000){
// discount is 5% since the amount to be paid exceeds $1000
    discount=0.05*amount_paid;
    amount_paid-=discount;
}

推荐阅读