1025: coin statistics

Source: Internet
Author: User
Description

Assume that a pile of n coins, consisting of 1, 2, and 5, have a total face value of M, evaluate the total number of possible combinations (the number of coins with a certain nominal value can be 0 ).

Input Format

The first line of the input data has a positive integer T, indicating that there are T groups of test data. In the next t row, each row has two numbers n, m, n, and M.

Output

For each group of test data, please output the number of possible combinations, each group of output occupies one line.

Sample Input

2
3 5
4 8

Sample output

1
2

The idea of this question is similar to that of chicken, rabbit, and cage, so it is not difficult to think of using a few for loops to perform exhaustive actions on possible values. Below is an algorithm I have written, which is slightly optimized in exhaustive actions.

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

There are still errors after submission, and no where is found yet. The following is an official algorithm with some optimizations.

 1 #include<stdio.h> 2  3 int main() 4 { 5         int t,n,m,c1,c2,c5,k; 6         scanf("%d",&t); 7         while(t--) 8         { 9                 scanf("%d%d",&n,&m);10                 k=0;11                 for(c5=0;5*c5<=m;c5++)12                         for(c2=0;2*c2+5*c5<=m;c2++)13                         {14                                 c1=m-5*c5-2*c2;15                                 if(c1+c2+c5==n)16                                         k++;17                         }18                 printf("%d\n",k);19         }20         return 0;21 }

It is also worth mentioning that this question is similar to the algorithm of the 1023-hookay black store.

In addition, it was modified according to the official website. Strange.

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

Finally, I finally found the problem. k = n-I-j; because there is no limit on the initial j of I, K may be a negative value.

The following code is AC

 1 #include <stdio.h> 2 int main(void) 3 { 4     int n,m; 5     int time; 6      7     scanf("%d",&time); 8     while(time--) 9     {    10         11         int count=0;12         scanf("%d %d",&n,&m);13         int i,j,k,total;14         15         for(i=0;5*i<=m;i++)16         {17             18             for(j=0;2*j<=m;j++)19                 {20                     k=n-j-i;                21                     total=k*1+j*2+i*5;22                     if(total==m&&k>=0)23                     {24                         25                         count++;26                     }27                             28                 }29                 30         }31         printf("%d\n",count);32     }33     return 0;34 }

 

1025: coin statistics

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.