统计硬币(HDU 2566)

/*

统计硬币

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2495    Accepted Submission(s): 1726

Problem Description
假设一堆由1分、2分、5分组成的n个硬币总面值为m分,求一共有多少种可能的组合方式(某种面值的硬币可以数量可以为0)。
 
Input
输入数据第一行有一个正整数T,表示有T组测试数据; 接下来的T行,每行有两个数n,m,n和m的含义同上。
 
Output
对于每组测试数据,请输出可能的组合方式数; 每组输出占一行。
*/

#include<iostream>

using namespace std;

int main()

{

         int a;

         cin>>a;

         int n,m,tag,i,j,k,num;

         while(a--)

         {

                   num=0;

                   cin>>n>>m;

                   tag=0;

                   for(i=0;i<=n;i++)

                   {

                            if((num+1>m)&&(i!=0))

                                     break;

                            for(j=0;j<=n;j++)

                            {

                                     if((2+num>m)&&(j!=0))

                                               break;

                                     for(k=0;k<=n;k++)

                                     {

                                               if((i*1+j*2+k*5)==m&&((i+j+k)==n))

                                                        num++;

                                               if((i*1+j*2+k*5)>m)

                                                        break;

                                     }

                            }

                   }

                   cout<<num<<endl;

                   num=0;

         }

         return 0;

}

posted @ 2012-11-25 14:02  ♂咱說 ろ算  阅读(228)  评论(0编辑  收藏  举报