eggs
Description:
Erin买了不少鸡蛋,她发现一天吃不完这么多,于是决定把n个同样的鸡蛋放在m个同样的篮子里,允许有的篮子空着不放,请问共有多少种不同的放法呢?
注意:2,1,1和1,2,1 是同一种分法。
第一行是测试数据的数目t(0 <= t <= 20)。以下每行均包含二个整数m和n,以空格分开。1<=m,n<=10。
对输入的每组数据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 = ;
scanf("%d", &t);
for (i = ; i < t; i++) {
scanf("%d%d", &m, &n);
result = egg(m, n);
printf("%d\n", result);
}
return ;
}
int egg(int m, int n) {
if (m == || n == ) {
return ;
}
if (n <= m) {
return + egg(n-, n);
} else {
return egg(m-, 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 = ;
scanf("%d", &t);
while (t--) {
scanf("%d %d", &m, &n);
result = egg(m , n);
printf("%d\n", result);
}
return ;
} int egg(int m, int n) {
if (m == || n == )
return ;
if (n < )
return ;
return egg(m-, n) + egg(m, n-m);
}
eggs的更多相关文章
- [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. I ...
- SZU:B85 Alec's Eggs
Description Eggs Alec has a lot of eggs. One day, he want to sort them in a ascending sequence by we ...
- cooking eggs
1: what is egg? what's the shape of it in details? 2: can egg run like this http://item.taobao.com/i ...
- Golden Eggs HDU - 3820(最小割)
Golden Eggs Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 3820 Golden Eggs (SAP | Dinic)
Golden Eggs Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 100 floors 2 eggs
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- 254. Drop Eggs【LintCode java】
Description There is a building of n floors. If an egg drops from the k th floor or above, it will b ...
- HDU 3820 Golden Eggs( 最小割 奇特建图)经典
Golden Eggs Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 贪心 Codeforces Round #173 (Div. 2) B. Painting Eggs
题目传送门 /* 题意:给出一种方案使得abs (A - G) <= 500,否则输出-1 贪心:每次选取使他们相差最小的,然而并没有-1:) */ #include <cstdio> ...
随机推荐
- 在织梦dedecms中实现“文章标题-栏目名称-网站名”导航
本文介绍了在dedecms中,实现文章标题-栏目名称-网站名 导航的方法,有需要的朋友参考下. 在dedecms中实现“文章标题-栏目名称-网站名”导航的方法. 第一种: 在/include/in ...
- MYSQL初级学习笔记六:子查询!(视频序号:初级_42 | 43)
知识点八:子查询(42) 什么是子查询: 子查询是将一个查询语句嵌套在另一个查询语句中.内层查询语句的查询结果,可以作为外层查询语句提供条件. 引发子查询的情况: 使用[NOT] IN 的子查询 -- ...
- 666 专题五 AC自动机
Problem A.Keywords Search d.n个关键字,1段描述,求描述中出现了多少关键字 s. c. /* ac自动机模板 n个关键字,1段描述,求描述中出现了多少关键字 */ #inc ...
- 设置linux服务器下开放端口
查询 netstat -anp 所有开放端口信息 二.关闭端口号: iptables -A OUTPUT -p tcp --dport 端口号-j DROP 三.打开端口号: iptables -A ...
- GDUT 积木积水 2*n 时间复杂度
题意 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元,于是他把这个三维的现实问 ...
- 书写优雅的shell脚本(插曲)- /proc/${pid}/status
Linux中/proc/[pid]/status详细说明 博客分类: OS Linux多线程 [root@localhost ~]# cat /proc/self/status Name: cat ...
- [Selenium] Explicit wait 方法
(1) new WebDriverWait(driver, 10). until(ExpectedConditions.elementToBeClickable(locator)); (2) ne ...
- Objective-C中的+initialize和+load
写在前面 近几天花了一些时间了解了一下Objective-C runtime相关的东西,其中涉及到了+load方法,譬如method swizzling通常在category的+load方法中完成.之 ...
- Linux 开机引导和启动过程详解
你是否曾经对操作系统为何能够执行应用程序而感到疑惑?那么本文将为你揭开操作系统引导与启动的面纱. 理解操作系统开机引导和启动过程对于配置操作系统和解决相关启动问题是至关重要的.该文章陈述了 GRUB2 ...
- linux的存储结构
在linux中存储结构如下: Linux系统中的文件存储结构 那么在linux中每个目录都是什么含义呢 在linux中相对路径和绝对路径是必须要了解的一个概念 绝对路径(absolute path): ...