Design ideas:
Through the calculation found that the purchase just press (x*5+y) (0<y<5), X set of 5 in addition to buy Y (0<y<5) the lowest price, but there is a column outside, in the case of x*5+2*4 this, according to X set of 5, 2 sets of 4 this purchase than x sets of 5 this separately buy y cheaper. So it can be programmed by situation analysis.
Program code:
1#include <iostream>2 using namespacestd;3 4 voidMain ()5 {6 intx;7cout <<"Please enter the number you want to buy:"<<Endl;8CIN >>x;9 Ten inti; Onei = x/5; A - if(X <5) - { the Switch(x) - { - Case 1: -cout <<"Buy 1 book The lowest price is 8 yuan"<<Endl; + Break; - Case 2: +cout <<"Buy 2 book the lowest price for"<< x*8*0.95<<"Yuan"<<Endl; A Break; at Case 3: -cout <<"Buy 3 book the lowest price for"<< x*8*0.9<<"Yuan"<<Endl; - Break; - Case 4: -cout <<"Buy 4 book the lowest price for"<< x*8*0.8<<"Yuan"<<Endl; - } in } - Else{ to Switch(%5) + { - Case 0: thecout <<"buy"<< I <<"set of 5 copies of the"<<Endl; *cout <<"the lowest price is:"<< i*8*5*0.75<<Endl; $ Break;Panax Notoginseng Case 1: -cout <<"buy"<< I <<"set of 5 copies of the"<<Endl; thecout <<"Plus"<< x%5<<"this"<<Endl; +cout <<"the lowest price is:"<< i*8*5*0.75+ (Percent5)*8<<Endl; A Break; the Case 2: +cout <<"buy"<< I <<"set of 5 copies of the"<<Endl; -cout <<"Plus"<< x%5<<"this"<<Endl; $cout <<"the lowest price is:"<< i*8*5*0.75+ (Percent5)*8*0.95<<Endl; $ Break; - Case 3: -cout <<"buy"<< I-1<<"set of 5 copies of the"<<Endl; thecout <<"plus 2 sets of 4 copies ."<<Endl; -cout <<"the lowest price is:"<< (I-1)*8*5*0.75+2*4*8*0.8<<Endl;Wuyi Break; the Case 4: -cout <<"buy"<< I <<"set of 5 copies of the"<<Endl; Wucout <<"Plus"<< x%5<<"this"<<Endl; -cout <<"the lowest price is:"<< i*8*5*0.75+ (Percent5)*8*0.8<<Endl; About } $ } -}
Results:
Personal Summary:
The problem is quite simple, but how to make it easier is the best. Through analysis, this method was finally selected. It feels so simple.
About the purchase of the Harry Potter book