杭电2079
此题的思路比较简单,就是用的穷举方法,为什么用这个方法呢?呵呵!其实你可以
清楚的看到n(1 <= n <= 40),k(1 <= k <= 8)的取值范围,用穷举的办法即可去、
轻松ac,而且还不会超时!呵呵!
首先统计各个学分的个数,然后依次穷举即可!
#include<iostream>
using namespace std;;
int main()
{
int T,n,k,a1,a2,a3,a4,a5,a6,a7,a8,i1,i2,i3,i4,i5,i6,i7,i8,a,b,sum,i;
cin>>T;
getchar();
while(T--)
{
a1=a2=a3=a4=a5=a6=a7=a8=sum=0;
cin>>n>>k;
for(i=0;i<k;i++)
{
cin>>a>>b;
switch(a)
{
case 1: a1=a1+b;break;
case 2: a2=a2+b;break;
case 3: a3=a3+b; break;
case 4: a4=a4+b;break;
case 5: a5=a5+b;break;
case 6: a6=a6+b;break;
case 7: a7=a7+b;break;
case 8: a8=a8+b;break;
}
}
for(i8=0;i8<=a8;i8++)
for(i7=0;i7<=a7;i7++)
for(i6=0;i6<=a6;i6++)
for(i5=0;i5<=a5;i5++)
for(i4=0;i4<=a4;i4++)
for(i3=0;i3<=a3;i3++)
for(i2=0;i2<=a2;i2++)
for(i1=0;i1<=a1;i1++)
if(i1*1+i2*2+i3*3+i4*4+i5*5+i6*6+i7*7+i8*8==n)
sum++;
printf("%d\n",sum);
}
return 0;
}