Problem:
A bookstore to the "Harry Potter" promotional activities, a total of 5 volumes. Assuming that each volume of the individual sales price of 8 yuan, if the reader to buy a different two volumes, you can deduct 5% of the cost, three volumes more, the benefits are as follows:
Analysis of all possible situations:
It can be seen that the code is written as follows;
#include <stdio.h>intchange[5] = { -, -,Ten,5,0}; //find the largest value in an array//used to remove big savingsintMax_index (inta[5]){ intmax=0; inti; for(i=0;i<5; i++){ if(Max <A[i]) Max=A[i]; } returnMax; } //Bubble Sort//minimum representation for conversion (1,2,2,2,2), (2,2,2,2,1)voidBubbleint*a,intN) {inti,j,temp; for(i=0; i<n-1; i++) for(j=i+1; j<n;j++) if(a[i]<A[j]) {Temp=A[i]; A[i]=A[j]; A[J]=temp; } } //Recursive FunctionsintFuncinta[5]){ inti,j,k; intbuf[5]; intcash[5]; for(i=0;i<5; i++) {Cash[i]=0; //Buf[i] = a[i]; } //Exit Criteria if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]&&a[3]==a[4]&&a[4] ==0){ return 0; } //Sort conversions Minimum representationBubble (A,5); //5 Cycles for(i=1; i<=5; i++){ if(a[5-I.] >=1){ for(k=0;k<5; k++) {Buf[k]=A[k]; } //minus One for(j=0; j<=5-i;j++) Buf[j]= buf[j]-1; //continue to recursioncash[i-1] = (5-i+1)*8*change[i-1]+func (BUF); } } //returns the maximum value returnMax_index (Cash); } intMain () {intb=0; inta[5] = {0,0,0,0,0}; printf ("This procedure assumes that each book is priced at 8 yuan, please enter the number of books:"); scanf ("%d",&b); for(intI=0; i<b;i++) {A[i%5]+=1; } floatresult =0; Result=func (a); printf ("total savings%2.2f yuan! \ n", result/ -); }
Run:
Buying a book question