关于《哈利波特》书的购买方案
思路:
购买时只需按(x*5+y)(0<y<5),x套5本另外买y本(0<y<5)时价格最低。
代码:
#include <iostream> using namespace std; void main() { int n; cout << "请输入要购买书的本数:" << endl; cin >> n; int i; i = n/5; if (n < 5) { switch(n) { case 1: cout << "买1本书最低价格为8元"<< endl; break; case 2: cout << "买2本书最低价格为" << n*8*0.95 << "元"<< endl; break; case 3: cout << "买3本书最低价格为" << n*8*0.9 << "元"<< endl; break; case 4: cout << "买4本书最低价格为" << n*8*0.8 << "元"<< endl; } } else{ switch(n%5) { case 0: cout << "买" << i << "套5本的" << endl; cout << "最低价格为:" << i*8*5*0.75<< endl; break; case 1: cout << "买" << i << "套5本的" << endl; cout << "外加" << n%5 << "本" << endl; cout << "最低价格为:" << i*8*5*0.75 + (n%5)*8<< endl; break; case 2: cout << "买" << i << "套5本的" << endl; cout << "外加" << n%5 << "本" << endl; cout << "最低价格为:" << i*8*5*0.75 + (n%5)*8*0.95 << endl; break; case 3: cout << "买" << i-1 << "套5本的" << endl; cout << "外加2套4本" << endl; cout << "最低价格为:" << (i-1)*8*5*0.75 + 2*4*8*0.8 << endl; break; case 4: cout << "买" << i << "套5本的" << endl; cout << "外加" << n%5 << "本" << endl; cout << "最低价格为:" << i*8*5*0.75 + (n%5)*8*0.8 << endl; } } }