zoukankan      html  css  js  c++  java
  • poj1664~递归

    放苹果
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 30797   Accepted: 19444

    Description

    把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。

    Input

    第一行是测试数据的数目t(0 <= t <= 20)。以下每行均包含二个整数M和N,以空格分开。1<=M,N<=10。

    Output

    对输入的每组数据M和N,用一行输出相应的K。

    Sample Input

    1
    7 3
    

    Sample Output

    8

    Source

     
     
     
    #include<iostream>
    using namespace std;
    
    int fun(int x,int y)
    {
        if(x<0) return 0;
        if(x==0||y==1) return 1;
        else return fun(x-y,y)+fun(x,y-1);
    
    }
    
    int main()
    {
        int t;
        while(cin>>t){
        while(t--)
        {
            int m,n;
            cin>>m>>n;
            int k=fun(m,n);
            cout<<k<<endl;
        }
        }
        return 0;
    }
    

      1、要么最少的盘子放了一个,那么先每个都放一个,剩下的x-y随便放

      2、要么最少的盘子不放,那么把x个都放到y-1个盘子里面。

  • 相关阅读:
    博客园代码
    前端
    1338. Reduce Array Size to The Half
    1220. Count Vowels Permutation
    363. Max Sum of Rectangle No Larger Than K
    366. Find Leaves of Binary Tree
    443. String Compression
    8 · Rotate String
    886. Possible Bipartition
    LT 183 wood cut
  • 原文地址:https://www.cnblogs.com/hellohacker/p/5771453.html
Copyright © 2011-2022 走看看