我们完全可以在书的数量大于5时,直接除于5,得到余数,当数目可以变为x*5+2*4时,则购入X套与2套4本,这是极好的。。。。
1 #include <iostream> 2 using namespace std; 3 4 void main() 5 { 6 int x; 7 cout << "请输入要购买的本数:" << endl; 8 cin >> x; 9 10 int i; 11 i = x/5; 12 13 if (x < 5) 14 { 15 switch(x) 16 { 17 case 1: 18 cout << "买1本书最低价格为8元"<< endl; 19 break; 20 case 2: 21 cout << "买2本书最低价格为" << x*8*0.95 << "元"<< endl; 22 break; 23 case 3: 24 cout << "买3本书最低价格为" << x*8*0.9 << "元"<< endl; 25 break; 26 case 4: 27 cout << "买4本书最低价格为" << x*8*0.8 << "元"<< endl; 28 } 29 } 30 else{ 31 switch(x%5) 32 { 33 case 0: 34 cout << "买" << i << "套5本的" << endl; 35 cout << "最低价格为:" << i*8*5*0.75<< endl; 36 break; 37 case 1: 38 cout << "买" << i << "套5本的" << endl; 39 cout << "外加" << x%5 << "本" << endl; 40 cout << "最低价格为:" << i*8*5*0.75 + (x%5)*8<< endl; 41 break; 42 case 2: 43 cout << "买" << i << "套5本的" << endl; 44 cout << "外加" << x%5 << "本" << endl; 45 cout << "最低价格为:" << i*8*5*0.75 + (x%5)*8*0.95 << endl; 46 break; 47 case 3://特殊情况 48 cout << "买" << i-1 << "套5本的" << endl; 49 cout << "外加2套4本" << endl; 50 cout << "最低价格为:" << (i-1)*8*5*0.75 + 2*4*8*0.8 << endl; 51 break; 52 case 4: 53 cout << "买" << i << "套5本的" << endl; 54 cout << "外加" << x%5 << "本" << endl; 55 cout << "最低价格为:" << i*8*5*0.75 + (x%5)*8*0.8 << endl; 56 } 57 } 58 }
时间: 2024-10-09 22:35:06