First, the topic requirements
Bookstore for the "Harry Potter" series of books promotional activities, a total of 5 volumes, with numbers 0, 1, 2, 3, 4, a single volume of 8 yuan, specific preferential information as follows:
Discount on this number
2 5%
3 10%
4 20%
5 25%
Depending on the number of volumes purchased, the program is able to purchase a batch of books at its lowest price, with the corresponding offers.
Second, design ideas
Existing five books in the offer, already in detail in the table, buy 6 to 9 books, if five of them enjoy 25% discount, we left the table to find the corresponding preferential information, that is, there is a combination of forms, so that the price of the book to buy the lowest.
Third, the Code
1#include <iostream.h>2 voidMain ()3 {4 intNum//this number5 intRecord//Business6 intremainder;//remainder7 DoublePrice ;8 intn=1;9 while(n)Ten { Onecout<<"Please enter the number of books you buy:"<<Endl; ACin>>num; -record=num/5; -remainder=num%5; the if(record>=2) - { -record--; -remainder=remainder+5; + } - if(num<=5) + { A Switch(num) at { - Case 0: -Price=0; - Break; - Case 1: -Price=8; in Break; - Case 2: toPrice= -*(1-0.05); + Break; - Case 3: thePrice= -*(1-0.1); * Break; $ Case 4:Panax NotoginsengPrice= +*(1-0.2); - Break; the Case 5: +Price= +*(1-0.25); A Break; the default: + Break; - } $ } $ Else - { - Switch(remainder) the { - Case 5:Wuyiprice=record* +*(1-0.25)+ +*(1-0.25); the Break; - Case 6: Wuprice=record* +*(1-0.25)+ +*(1-0.25)+8; - Break; About Case 7: $price=record* +*(1-0.25)+ +*(1-0.2)+ -*(1-0.05); - Break; - Case 8: -price=record* +*(1-0.25)+ +*(1-0.2)*2; A Break; + Case 9: theprice=record* +*(1-0.25)+ +*(1-0.25)+ +*(1-0.2); - Break; $ default: the Break; the } the } thecout<<"buy"<<num<<"The lowest price of this book is:"<<price<<"Yuan"<<Endl; -cout<<"continue entering 1, exit input 0"<<Endl; inCin>>N; the } the}
Iv. Results of operation
Buying "Harry Potter" books questions