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;
    }
}

 

转载于:https://www.cnblogs.com/w-xibao/p/8018561.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值