Stamps combination

Stamps combination (Beijing Institute of Technology mooc)

Title Contents:

We have to send a letter postage, the post office has some small denomination stamps, these small denomination stamps or in a combination of a few, to meet different postage different message.
Now, the post office there are four different denominations of stamps. Up to 5 stamp affixed on each envelope, denomination may be the same or different.

Input formats:

Face value of four kinds of stamps.

Output formats:

With a par value consisting of four postage largest from the beginning of a continuous range.

Note:
If the result is 10, this indicates that the combination of four stamps may 1,2,3,4,5,6,7,8,9,10 the postage.

Glossary:
Postage: send something that you need to spend much money.
Stamp denominations: by flower paper issued by the State with a fixed price, known stamps.

If you send something, post office, said the weight, to tell you 240 points. So you have to stick stamps. If the current post office stamp has a face value of 80 points, 50 points, 20 points and 10 points four, you can obtain different combinations of postage 240 using, for example: using three 80 may Couchu 240 minutes; or 24 min 10 min 240 may be pooled. Obviously a combination of different stamps give the same postage.

Sample input:

1 4 12 21

Sample output:

The max is 71.
 1 #include<stdio.h>
 2 int f(int *m,int a1,int a2,int a3,int a4)
 3 {
 4     int i,j,k,p;
 5     for( i = 0 ; i <= 5 ; i++ )
 6     { 
 7         for( j = 0 ; j <= 5 - i ; j++ ) 
 8         { 
 9             for( k = 0 ; k <= 5 - i - j ; k++ ) 
10             { 
11                 for( p = 0 ; p <= 5 - i - j - k ; p++ ) 
12                 {
13                     if( a1 * i + a2 * j + a3 * k + a4 * p == *m)
14                         return 1;              
15                 }            
16             } 
17         } 
18     } 
19     return 0;    
20 }
21 int main()
22 {
23     int a1, a2, a3, a4, m=1;
24     scanf("%d%d%d%d",&a1,&a2,&a3,&a4);
25     while(f(&m,a1,a2,a3,a4))
26         m++; 
27     printf("The max is %d.\n",--m);
28     return 0;
29 }

 

Guess you like

Origin www.cnblogs.com/GoldenEllipsis/p/11614160.html