ACM 超级楼梯 发工资
超级楼梯
有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?
Input
输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。
Output
对于每个测试实例,请输出不同走法的数量
Sample Input
2
2
3
Sample Output
1
2
代码:
public class ChaoJiLouTi { public static void main(String[] args) {
System.out.println(method2(4));
}
public static int method2(int n){
//限定n的取值范围
if(n<1 || n>40){
return -1;
}
int count;
if (n == 0 || n == 1) { //假定站在第一层台阶
count = 0;
} else if (n == 2) {
count = 1;
} else if (n == 3) {
count = 2;
} else { //递归调用
count = method2(n - 1) + method2(n - 2);
}
return count;
}
}
代码2:
public class Exam1207_2 { public static void main(String[] args) {
// TODO Auto-generated method stub
//6级楼梯5步
//1 1 1 1 1
//2 1 1 1(1 2 1 1 - 1 1 2 1 - 1 1 1 2)
//2 2 1(2 1 2 - 1 2 2 )
int m=6;
m--; //m=5
int sum=1; int hei=0;
int bai=0;
for(int i=1;i<=m/2;i++){
//m个黑球和n个白球,一共有多少种取法
hei=i;
bai=m-i*2;
sum+=method(hei,bai); //对于i个2,返回其组合的个数
}
System.out.println(sum);
} //对于m个黑球和n个白球,一共有多少种取法
public static int method(int m,int n) {
n=m+n;
return jieCheng(n)/(jieCheng(m)*jieCheng(n-m));
} //对于传入的任意x,返回x的阶乘
public static int jieCheng(int x){
int sum=1;
for(int i=1;i<=x;i++){
sum*=i;
}
return sum;
}
}
发工资
Problem Description
作为企业的老板,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵
但是对于财务处的工作人员来说,这一天则是很忙碌的一天,财务处的小胡最近就在考虑一个问题:如果每个员工的工资额都知道,最少需要准备多少张人民币,才能在给每位员工发工资的时候都不用找零呢?
这里假设员工的工资都是正整数,单位元,人民币一共有100元、50元、10元、5元、2元和1元六种。
Input
输入数据包含多个测试实例,每个测试实例的第一行是一个整数n(n<100),表示员工的人数,然后是n个员工的工资。
n=0表示输入的结束,不做处理。
Output
对于每个测试实例输出一个整数x,表示至少需要准备的人民币张数。每个输出占一行。
Sample Input
3
1 2 3
0
Sample Output
4
public class Exam1207_5 {
public static void main(String[] args) {
int[] ins=new int[]{1,2,3}; //1 2 5
int sum=0;
for(int i=0;i<ins.length;i++){
sum+=method(ins[i]);
}
System.out.println(sum);
}
private static int method(int x) {
//318(3 100 | 1 10 | 1 5 | 1 2 | 1 1)
int count=0;
if(x>=100){
while(x>=100){
x-=100;
count++;
}
}
if(x>=50){
while(x>=50){
x-=50;
count++;
}
}
if(x>=10){
while(x>=10){
x-=10;
count++;
}
}
if(x>=5){
while(x>=5){
x-=5;
count++;
}
}
if(x>=2){
while(x>=2){
x-=2;
count++;
}
}
if(x>=1){
while(x>=1){
x-=1;
count++;
}
}
return count;
}
}
ACM 超级楼梯 发工资的更多相关文章
- 2041 ACM 超级楼梯
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2041 数学问题,找规律,可以先假设全一步,然后一个两步的,两个两步的~~.很容易发现规律:F[N]=F[N ...
- Hdu2041 超级楼梯 (斐波那契数列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2041 超级楼梯 Time Limit: 2000/1000 MS (Java/Others) M ...
- HDOJ2041_超级楼梯(斐波拉契数列)
正常简单题:通过仔细观察推断即可看出这是一个斐波拉契数列的题目. HDOJ2041_超级楼梯 在做这题的时候我误入了思维盲区,只想着什么方法可以解决,没有看出是斐波拉契数列.因此第一次用组合数方法打了 ...
- HDU 2021 发工资咯:)
http://acm.hdu.edu.cn/showproblem.php?pid=2021 Problem Description 作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的 ...
- HDU 2021 发工资咯:)(最水贪心)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2021 发工资咯:) Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 2041:超级楼梯(水题,递归)
超级楼梯 Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Su ...
- 超级楼梯[HDU2041]
超级楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDOJ2021发工资咯:)
发工资咯:) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDOJ 2021 发工资咯:)(利用了一种取余的思想)
Problem Description 作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵 但是对于学校财务处的工作人员来说,这一天则是很忙碌的一天,财务处 ...
随机推荐
- Android:ScaleType与Matrix相关
关于ScaleType,网上介绍这个枚举对象的文章很多了,不过基本都只是介绍了它的效果.我在做可缩放移动的ImageView时,为了实现图片的缩放和拖动,需要记录图片的原始Matrix,在使用过程中发 ...
- Shift Operations on C
The C standard doesn't precisely define which type of right shift should be used. For unsigned data, ...
- keil5破解
没有破解之前的keil只能编译限制大小的代码,72K好像我忘了?太长的话会报错. 注册机网址:http://bbs.armfly.com/read.php?tid=2346 1.在keil5左上角的F ...
- mongodb主从复制 副本集(六)
主从复制副本集 8888.conf dbpath = D:\software\MongoDBDATA\07\8888 #主数据库地址port = 8888 #主数据库端口号bind_ip = 127. ...
- WPF 绑定以基础数据类型为集合的无字段名的数据源
WPF 绑定以基础数据类型为集合的无字段名的数据源 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-21 我们在控件的数据绑定 ...
- LeetCode Crack Note --- 1. Two Sum
Discription Given an array of integers, return indices of the two numbers such that they add up to a ...
- Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration
在Apache的配置文件 httpd.conf 中开启 LoadModule headers_module modules/mod_headers.so 即可解决这个问题.
- 搭建Git Server - 个人开发简单搭建
###################### 教程一 ####################### 1. 创建git用户和用户组 #新建一个git用户组 sudo groupadd git #新建一 ...
- excel解析的几种实现方法
- springmvc jpa
昨天帮同学搭建了一个springmvc+jpa+beetl模板引擎的项目环境,供参考. https://files.cnblogs.com/files/startnow/lntu-demo.zip 数 ...