1. 程式人生 > 實用技巧 >企業發放的獎金根據利潤提成。利潤I低於或等於100000元的,獎金可提成10%;利潤高於100000元,低於200000元(100000<I≤200000)時,低於100000元的部分按10%提成,高於100000元的部分

企業發放的獎金根據利潤提成。利潤I低於或等於100000元的,獎金可提成10%;利潤高於100000元,低於200000元(100000<I≤200000)時,低於100000元的部分按10%提成,高於100000元的部分

企業發放的獎金根據利潤提成。利潤I低於或等於100000元的,獎金可提成10%;利潤高於100000元,低於200000元(100000<I≤200000)時,低於100000元的部分按10%提成,高於100000元的部分,可提成7. 5%;200000<I≤400000時,低於200000元的部分仍按上述辦法提成(下同)。高於200000元的部分按5%提成;400000<<I≤600000元時,高於400000元的部分按3%提成;600000<1≤1000000時,高於600000元的部分按1.5%提成;I>1000000時,超過1000000元的部分按1%提成。從鍵盤輸入當月利潤I,求應發獎金總數。要求:(1) 使用if語句編寫程式。(2) 使用switch語句編寫程式。

(1) 使用if語句編寫程式。

解題思路: 先將每一檔的最大獎金算出來,在某一個區間時,則那小於這一檔的獎金加上多出部分的獎金即可,例如:

先列出100000檔的獎金是10000,則180000就是10000 + (180000-100000) * 0.075;

列出200000檔的獎金是第一檔加上多出100000部分的7.5%得到17500,則300000就是17500 + (300000-200000)*0.05;

答案:

#include <stdio.h>
int main()
{
	double I, salary = 0;
	printf("enter performance:");
	scanf_s("%lf", &I);
	if (I < 0) {
		printf("請輸入一個正數\n");
		system("pause");
		return -1;
	}
	double salary1 = 100000 * 0.1;//10萬的獎金
	double salary2 = (200000 - 100000) * 0.075 + salary1;//20萬的獎金
	double salary3 = (400000 - 200000) * 0.05 + salary2;//40萬的獎金
	double salary4 = (600000 - 400000) * 0.03 + salary3;//60萬的獎金
	double salary5 = (1000000 - 600000) * 0.015 + salary4;//100萬的獎金
	if (I <= 100000) {
		salary = I * 0.1;//小於100000按10%提成
	}else if (I > 100000 && I <= 200000) {
		salary = salary1 + (I - 100000) * 0.075;//多出10萬的按比例計算,加上10w的獎金
	}else if (I > 200000 && I <= 400000) {
		salary = salary2 + (I - 200000) * 0.05;//多出20萬的按比例計算,加上20w的獎金
	}else if (I > 400000 && I <= 600000) {
		salary = salary3 + (I - 400000) * 0.03;//多出40萬的按比例計算,加上40w的獎金
	}else if (I > 600000 && I <= 1000000) {
		salary = salary4 + (I - 600000) * 0.015;//多出60萬的按比例計算,加上60w的獎金
	}else if (I > 1000000){
		salary = salary5 + (I - 1000000) * 0.01;//多出100萬的按比例計算,加上100w的獎金
	}
	printf("salary:%f\n", salary);
	system("pause");
	return 0;
}

(2) 使用switch語句編寫程式。

解題思路: 與第一題思路沒有太大差別,區別在於switch語句的case子句中需要是一個常量整數,並且switch中若子句中沒有break將循序向下執行,直到遇到break才會跳出switch語句,如果這時候將利潤除以10w,則得到0~9的數字,其中0表示小於10w,1表示介於10~20w,2、3表示介於20~40w,4、5表示介於40~60w,6、7、8、9表示介於60~100w,否則就是大於100w

答案:

#include <stdio.h>
int main()
{
	double I, salary = 0;
	printf("enter performance:");
	scanf_s("%lf", &I);
	if (I < 0) {
		printf("請輸入一個正數\n");
		system("pause");
		return -1;
	}
	double salary1 = 100000 * 0.1;//大於100000時0~100000的獎金
	double salary2 = (200000 - 100000) * 0.075 + salary1;//大於200000時0~20萬的獎金
	double salary3 = (400000 - 200000) * 0.05 + salary2;//大於400000時0~40萬的獎金
	double salary4 = (600000 - 400000) * 0.03 + salary3;//大於600000時0~60萬的獎金
	double salary5 = (1000000 - 600000) * 0.015 + salary4;//大於1000000時0~100萬的獎金
	int grade = I / 100000;
	switch(grade) {
		case 0:
			salary = I * 0.1; break;
		case 1:
			salary = salary1 + (I - 100000) * 0.075; break;
		case 2://會順序執行到下一個break處
		case 3:
			salary = salary2 + (I - 200000) * 0.05; break;
		case 4:
		case 5:
			salary = salary3 + (I - 400000) * 0.03; break;
		case 6:
		case 7:
		case 8:
		case 9:
			salary = salary4 + (I - 600000) * 0.015; break;
		default:
			salary = salary5 + (I - 1000000) * 0.01; break;
	}
	printf("salary:%f\n", salary);
	system("pause");
	return 0;
}