Classroom Practice Book promotion (greedy algorithm)

Source: Internet
Author: User

I. Topics

Bookstore for the "Harry Potter" series of books for promotional activities, a total of 5 volumes, with numbers 0, 1, 2, 3, 4, a single volume of 8 yuan, the specific discount is as follows: this number         & nbsp;         Discount                                      2                        5%                                     3                        10%                                     4                        20%                                     5                        25 %1 will correspond to different discount rules depending on the number of volumes purchased and this number. The singular book only corresponds to one discount rule, for example, two volume 1, one volume 2, you can enjoy 5% discount, another volume does not enjoy the discount. The 2 design algorithm calculates the lowest price for the reader to buy a batch of books. 3 requires the design idea, code implementation, implementation, personal summary in the form of blog post.   two. Design IdeasAccording to the discount and buy book quantity can know: 1)When purchasing 1,2,3,4,5, buy the best in volume number order; 2) When buying 5 of the integers, 5 volumes are bought together:5,5,5....... Best; 3) when purchasing 6,7,8,9, the best is: 6=1+5, 7=2+5, 8=4+4, 9 =5+44) When buying more than 10 books, will be the number of surplus, calculated when the purchase of 6, 7, 8, 9 book The minimum price, the rest of the direct purchase of 5 sets of discount price is the best solution. three. Code implementation
1 //ConsoleApplication6.cpp: Defines the entry point of the console application. 2 //3#include"stdafx.h"4 int_tmain (intARGC, _tchar*argv[])5 {6     return 0;7 }8#include <iostream>9 using namespacestd;Ten floatPrice ; One voidTanxin (intBook ) A { -     intYushu; -Yushu = Book%5; the     Switch(Yushu) -     { -      Case 0: -Price = Book *8*0.75; +          Break; -      Case 1: +Price = (Book-yushu) *8*0.75+8; A          Break; at      Case 2: -Price = (Book-yushu) *8*0.75+2*8*0.95; -          Break; -      Case 3: -Price = (Book-yushu) *8*0.75+4*8*0.8*2-5*8*0.75; -         if(book<5) in         { -Price + =0.4; to         } +          Break; -      Case 4: thePrice = (Book-yushu) *8*0.75+4*8*0.8; *          Break; $     default:Panax Notoginsengcout <<"error!"<<Endl; -  the     } + } A voidMain () the { +     intI=0; -      while(i==0) $     { $         intBook ; -cout <<"Buy this number (tip: Enter a positive integer):"; -CIN >>Book ; the tanxin (book); -cout <<"Lowest Price:"<< Price <<Endl;Wuyicout <<"do you want to buy it? (Tip: Enter 0 to continue, enter any key to exit)"; theCIN >>i; -     } Wu}

Four. Results Five. Summary for a negative number of this book hint error, but for this book too many, such as input 1.5 billion, the result shows an E. Through this experiment to self-improvement is very big.

Classroom Practice Book promotion (greedy algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.