Counting coin time limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3841 accepted submission (s): 2691
Problem 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 ).
The first line of 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 row.
Sample Input
23 54 8
Sample output
12
Authorlemon
Source Shaoxing top school of Information Technology-Program Design Competition of the second Computer Culture Festival
This question can be solved, but the brute-force code can be obtained directly:
<span style="font-size:14px;">#include<stdio.h>int main(){int t;scanf("%d",&t);while(t--){int i,j,k,n,m,s=0;scanf("%d%d",&n,&m);for(i=0;i<=m/5;i++){for(j=0;j<=m/2;j++){k=n-i-j;if(k>=0&&k+2*j+5*i==m)s++;}}printf("%d\n",s);}return 0;}</span>
The primary function uses a two-dimensional array. The Code is as follows:
<span style="font-size:14px;">#include<stdio.h>#include<string.h>int c1[110][110],c2[110][110];int main(){int i,j,k,l,n,m,t;scanf("%d",&t);while(t--){int sum=0;scanf("%d%d",&n,&m);memset(c1,0,sizeof(c1));memset(c2,0,sizeof(c2));for(i=0;i<=n;i++){c1[i][i]=1;}for(i=2;i<=5;i+=3){for(j=0;j<=m;j++)for(k=0;k*i+j<=m&&k<=n;k++){for(l=0;l+k<=n;l++)c2[k*i+j][k+l]+=c1[j][l];}for(j=0;j<=m;j++){for(k=0;k<=n;k++){c1[j][k]=c2[j][k];c2[j][k]=0;}}}printf("%d\n",c1[m][n]);}return 0;}</span>