解题报告:最多有8种学分的课,然后每种学分的课最多有10门,问要选学分为n的课一共有多少种选法,注意学分相同的课之间没有区别。
这题暴力可过,我用了8层循环,就简单了。听说可以用母函数,但没学过,看一下。

1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<cmath> 5 using namespace std; 6 int score[10]; 7 int main() { 8 int T,n,k,x,y; 9 scanf("%d",&T); 10 while(T--) { 11 scanf("%d%d",&n,&k); 12 memset(score,0,sizeof(score)); 13 while(k--) { 14 scanf("%d%d",&x,&y); 15 score[x] = y; 16 } 17 int tot = 0; 18 for(int a = 0;a<=score[1]&&a<=n;++a) 19 for(int b = 0;b<=score[2]&&a+2*b<=n;++b) 20 for(int c = 0;c<=score[3]&&a+2*b+3*c<=n;++c) 21 for(int d = 0;d<=score[4]&&a+2*b+3*c+4*d<=n;++d) 22 for(int e = 0;e<=score[5]&&a+2*b+3*c+4*d+5*e<=n;++e) 23 for(int f = 0;f<=score[6]&&a+2*b+3*c+4*d+5*e+6*f<=n;++f) 24 for(int g = 0;g<=score[7]&&a+2*b+3*c+4*d+5*e+6*f+7*g<=n;++g) 25 for(int h = 0;h<=score[8]&&a+2*b+3*c+4*d+5*e+6*f+7*g+8*h<=n;++h) 26 if(a+2*b+3*c+4*d+5*e+6*f+7*g+8*h == n) 27 tot++; 28 printf("%d ",tot); 29 } 30 return 0; 31 } 32