Hangdian 2566 (Master function brute force)

Source: Internet
Author: User
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>


 

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.