#include "stdafx.h"
/*
Section 与 Percent 对应关系:
利润<=Section[i] 时 , 提成为Percent[i]
*/
int Section[] = { 100000, 200000, 400000, 600000, 1000000 };
double Percent[] = { (double)10 / 100, 7.5 / 100, (double)5 / 100, (double)3 / 100, 1.5 / 100 };
//超过最大限定值时的提成比
double OtherPercent = (double)1 / 100;
int Len = sizeof(Section) / sizeof(int);
/*
说明:
index 在[0,Len) 范围内时,说明 Profit<= Section[Len-1] 或 Profit<=Section[0]
index = Len 时, 说明 Profit > Section[Len-1]
*/
int Split(const double& profit)
{
int index = -1, i=0;
if (profit <= 0.0)
return index;
for (; i < Len; i++)
{
if ((profit/Section[i]) >1.0)
continue;
index = i;
break;
}
//profit>Section[Len-1]时,index赋值为Len
if (i == Len)
index = i;
return index;
}
int _tmain(int argc, _TCHAR* argv[])
{
double Profit=0.0, GiveOut=0.0;
//输入利润
printf_s("利润:");
scanf_s("%lf", &Profit);
//计算应发放奖金
int index = Split(Profit);
double tempprofit = Profit;
while (index>=0)
{
//超过最大限定值
if (index == Len)
{
GiveOut += (tempprofit - Section[Len-1]) * OtherPercent;
tempprofit = Section[Len-1];
}
else
{
double temp;
//tempprofit 是否在 Section[0] 范围内
temp = (index-1)>=0? tempprofit-Section[index-1] : tempprofit-0;
GiveOut += temp * Percent[index];
tempprofit = (index-1)>=0 ? Section[index-1]:0;
}
index = Split(tempprofit);
}
printf_s("应发放奖金:%g\n", GiveOut);
return 0;
}
1634c-exercise-example2
利用 switch 的击穿现象,分享个计算方法。
#include <stdio.h>
int main(){
double d;
int money = 100000;
float res=0.0;
int flag;
scanf("%lf",&d);
flag = (int)(d/money);
flag = flag >10?10:flag;
switch(flag){
case 10:
res += (d-10*money)*0.01;
d = 10*money;
case 9:
case 8:
case 7:
case 6:
res += (d-6*money)*0.015;
d = 6*money;
case 5:
case 4:
res+= (d-4*money)*0.03;
d = 4*money;
case 3:
case 2:
res += (d-2*money)*0.05;
d = 2*money;
case 1:
res += (d-money)*0.075;
d = money;
case 0:
res += d *0.1;
}
printf("%.2f\n",res);
return 0;
}
#include <stdio.h>
#define WAN 10000
int main()
{
double I = 0; // 利润
double B = 0; // 奖金
scanf("%lf", &I);
I /= WAN;
if (I > 100 * WAN)
{
B += ((I - 100) * 0.01);
I = 100;
}
if (I > 60)
{
B += ((I - 60) * 0.015);
I = 60;
}
if (I > 40)
{
B += ((I - 40) * 0.03);
I = 40;
}
if (I > 20)
{
B += ((I - 20) * 0.05);
I = 20;
}
if (I > 10)
{
B += ((I - 10) * 0.075);
I = 10;
}
B += (I * 0.1);
printf("%lf", B);
}
1635c-exercise-example2
1634c-exercise-example2
利用 switch 的击穿现象,分享个计算方法。
1633c-exercise-example2
尝试使用循环优化代码的适用性:
1632c-exercise-example2
优化如下:
1631c-exercise-example1
深搜法(DFS)写此题: