eggs

Description:

Erin买了不少鸡蛋,她发现一天吃不完这么多,于是决定把n个同样的鸡蛋放在m个同样的篮子里,允许有的篮子空着不放,请问共有多少种不同的放法呢?

注意:2,1,1和1,2,1 是同一种分法。

Input

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

Output

对输入的每组数据m和n,用一行输出相应的结果。

例如:

Input:

4

3 8

4 7

2 4

4 2

Output:

10

11

3

2

(注意结尾有换行)


Hint:

尝试利用递归去分析解题,即不同情况下应该怎么返回。

可以尝试用树状图(然而我觉得用处不大)。

注意篮子可以空着不放,请先想明白示例中最后两个例子再做题。



我的代码:

#include<stdio.h>
int egg(int m, int n);
int main() {
    int t, m, n, i, result = 0;
    scanf("%d", &t);
    for (i = 0; i < t; i++) {
        scanf("%d%d", &m, &n);
        result = egg(m, n);
        printf("%d\n", result);
    }
    return 0;
}
int egg(int m, int n) {
    if (m == 1 || n == 1) {
        return 1;
    }
    if (n <= m) {
        return 1 + egg(n-1, n);
    } else {
        return egg(m-1, n) + egg(m, n-m);
    }
}


标答:

#include<stdio.h>
int egg(int m, int n);

int main() {
    int t;
    // m for baskets, n for eggs
    int m, n;
    int result = 0;
    scanf("%d", &t);
    while (t--) {
        scanf("%d %d", &m, &n);
        result = egg(m , n);
        printf("%d\n", result);
    }
    return 0;
}

int egg(int m, int n) {
    if (m == 1 || n == 0)
        return 1;
    if (n < 0)
        return 0;
    return egg(m-1, n) + egg(m, n-m);
}
时间: 2024-09-29 01:30:26

eggs的相关文章

hdoj 3820 Golden Eggs 【双二分图构造最小割模型】

Golden Eggs Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 505    Accepted Submission(s): 284 Problem Description There is a grid with N rows and M columns. In each cell you can choose to put

hdoj 3820 Golden Eggs 【最小割+拆点】

题目:hdoj 3820 Golden Eggs 题意:给出一个矩阵,然后当前有三种选择,放一个金蛋,放一个银蛋,或者不放,然后给出每个格子放金蛋或者银蛋的得分,如果金蛋相邻的话每个得分要减掉cost1,银蛋相邻的话每个减去cost2得分,问最大得分多少? 分析:做这个题目推荐先做hdoj 1659 ,3657点击打开链接 ,这个题目相当于前两个的融合在加点变化. 首先我们发现和前两个题目一样要求不能相邻,否则要减去一定的值,那么我们可以确定同样要用当前点的行列和的奇偶性建图. 那么我们可以按照

(eden)eggs

题目名称 eggs 题目描述 Erin买了不少鸡蛋,她发现一天吃不完这么多,于是决定把n个同样的鸡蛋放在m个同样的篮子里,允许有的篮子空着不放,请问共有多少种不同的放法呢? 注意:2,1,1和1,2,1 是同一种分法. Input 第一行是测试数据的数目t(0 <= t <= 20).以下每行均包含二个整数m和n,以空格分开.1<=m,n<=10. Output 对输入的每组数据m和n,用一行输出相应的结果. 例如: Input: 4 3 8 4 7 2 4 4 2 Output:

How to order your eggs?

learning how to order eggs is really important!sunny side up : 煎一面荷包蛋over easy egg: 煎双面,蛋黄没熟over medium egg:煎双面,蛋黄有一点硬fried egg:全熟鸡蛋scramble egg:炒蛋 boiled egg:煮蛋

Codeforces 282B. Painting Eggs

The Bitlandians are quite weird people. They have very peculiar customs. As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work. The kids are excited because just as is c

lintcode584- Drop Eggs II- medium

There is a building of n floors. If an egg drops from the k th floor or above, it will break. If it's dropped from any floor below, it will not break. You're given m eggs, Find k while minimize the number of drops for the worst case. Return the numbe

[CareerCup] 6.5 Drop Eggs 扔鸡蛋问题

6.5 There is a building of 100 floors. If an egg drops from the Nth floor or above, it will break. If it's dropped from any floor below, it will not break. You're given two eggs. Find N, while minimizing the number of drops for the worst case 这道题说有10

Eggs Dropping puzzle(2 eggs, 100 floors)

题目如下: You are given two eggs, and access to a 100-storey building. Both eggs are identical. The aim is to find out the highest floor from which an egg will not break when dropped out of a window from that floor. If an egg is dropped and does not brea

Golden Eggs HDU - 3820(最小割)

Golden Eggs Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 673    Accepted Submission(s): 400 Problem Description There is a grid with N rows and M columns. In each cell you can choose to put a