文章

28

粉丝

0

获赞

153

访问

3.6k

头像
利润提成 题解:绝对优雅的写法,别整一堆if else了
P1040 北京大学机试题
发布于2026年2月11日 14:35
阅读数 205

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
	double profit;
	cin>>profit;
    const double INF=1e9;
	double a[]={0,100000,200000,400000,600000,1000000,INF};
	double radio[]={0.1,0.075,0.05,0.03,0.015,0.01};
	int total;
	//想象一个数轴,左边是0,右边是无穷,共六个区间,从左往右算
	for(int i=0;i<6;i++){
		double low = a[i];//区间的左端
		double high = a[i+1];//区间的右端
        if(profit>low){
          double thisPart = min(profit, high) - low; //这段区间的利润
		  total += thisPart * radio[i];//这段区间利润的提成
        }else break;//总利润并未达到该区间
    }
    cout<<total;
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发