软件工程课堂练习购买图书
购书数量对10取余,每10本优惠的最大价钱都相同,然后再加之前以10为周期的优惠价钱,就可以算出总的优惠价格。
代码:
1 #include <iostream> 2 #include <iomanip> 3 using namespace std; 4 int main() { 5 const float discount[10] = {0,0,0.1,0.3,0.8,1.25,1.25,1.35,1.6,2.05}; 6 int count; 7 cout << "输入购书数量:"; 8 cin >> count; 9 if (count < 0) 10 { 11 cout << "Error ! Illegal Input" << endl; 12 exit(1); 13 } 14 float result = (count / 10)* 2.5 + discount[count % 10]; 15 cout << "优惠" <<setiosflags(ios::fixed)<<setprecision(2)<< 8 * result << endl; 16 }
运行截图