一本书:8元
2 :15.2元
3 :21.6
4 :25.6
5 :30
6 :(1 5)38
(2 4)40.8
(3 3)43.2
7 :(1 6)46
(2 5)45.2
(3 4)47.2
8 :(1 7)53.2
(2 6)53.2
(3 5) 51.6
(4 4)51.2
9 :(1 8)59.2
(2 7)60.4
(3 6)59.6
(4 5)55.6
10 :(5 5)60
11 :(1 10)68
(2 9)70.8
(3 8)72.8
(4 7)70.8
(5 6)68
12 :(1 11)76
(2 10)75.2
(3 9)77.2
(4 8)76.8
(5 7)75.2
猜测的规律:买n本书,1-5的就是买不同类型的,如果n>5,则n/5=m…..k
则最好的买书方案就是买 钱是m*30+k*不同的套餐
源代码:
1 //yanyuqing 2016.5.4 买书优惠 2 #include <iostream> 3 using namespace std; 4 5 double sum_price(int n) 6 { 7 double sum; 8 if(n==1) 9 { 10 sum=8; 11 } 12 else if(n==2) 13 { 14 sum=15.2; 15 } 16 else if(n==3) 17 { 18 sum=21.6; 19 } 20 else if(n==4) 21 { 22 sum=25.6; 23 } 24 else if(n==5) 25 { 26 sum=30; 27 } 28 else if(n>5) 29 { 30 int k; 31 k=n/5;//看是5的几倍,就买几次5的套餐 32 int m; 33 m=n-k*5;//余数 34 if(m==1) 35 { 36 sum=k*30+8; 37 } 38 else if(m==2) 39 { 40 sum=k*30+15.2; 41 } 42 else if(m==3) 43 { 44 sum=k*30+21.6; 45 } 46 else if(m==4) 47 { sum=k*30+25.6;} 48 } 49 return sum; 50 } 51 52 void main() 53 { 54 int num; 55 cout<<"请输入要买书的数量(本)"<<endl; 56 cin>>num; 57 double min_sumprice; 58 min_sumprice=sum_price(num); 59 cout<<"买"<<num<<"本书最优惠的价格为"<<min_sumprice<<"元"<<endl; 60 61 62 63 }