zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    阿里云RDS的mysql数据库连接
    DRF框架400错误信息处理(视图集)
    关于百度Tongji Api的文档补充
    Python几种创建list的方法的效率对比
    手把手教你使用python复杂一点点的装饰器
    Python爬虫常用模块,BeautifulSoup笔记
    LSSS 构造过程
    微信网站应用扫码登陆
    分享一个自用的 Inno Setup 软件打包脚本
    彻底解决:请求被中止: 未能创建 SSL/TLS 安全通道
  • 原文地址:https://www.cnblogs.com/w-xibao/p/8018561.html
Copyright © 2011-2022 走看看