zoukankan      html  css  js  c++  java
  • 算法5:售票员找零

    *售票员

    *新的《复仇者》电影刚刚上映!电影院售票处有很多人排起了长队。他们每人有一张100、50或25美元的钞票(每人只有一张纸币)。一张“复仇者”的票要25美元。

    *瓦西娅目前是一名职员。他想把票卖给这一行的每一个人。

    *如果瓦西娅一开始没有钱,而且严格按照人们排队的顺序售票,他能不能给每个人一张票,并找零钱?

    *如果瓦西娅可以把票卖给每个人,然后用他手头的钞票找零,返回YES。否则返回NO

        public static String Tickets(int[] peopleInLine, int lineNo) {
    
            final String RESULT_YES = "YES";
            final String RESULT_NO = "NO";
            String res = null;
    
            final int twentyFive = 25;
            final int fifty = 50;
            final int oneHundred = 100;
    
            int cnt_twentyFive = 0;
            int cnt_fifty = 0;
            int cnt_oneHundred = 0;
    
            //peopleInLine = new int[]{25, 25, 50};
            for (int i = 0; i < peopleInLine.length; i++) {
                if (peopleInLine[i] == twentyFive) {
                    cnt_twentyFive++;
                } else if (peopleInLine[i] == fifty) {
                    cnt_fifty++;
                } else if (peopleInLine[i] == oneHundred) {
                    cnt_oneHundred++;
                }
            }
            if (cnt_oneHundred == 0 && cnt_fifty == 0 && cnt_twentyFive != 0) {
                res = RESULT_YES;
            } else if (cnt_oneHundred == 0 && cnt_fifty != 0 && cnt_twentyFive != 0) {
                if (cnt_fifty <= cnt_twentyFive) {
                    res = RESULT_YES;
                } else {
                    res = RESULT_NO;
                }
            } else if (cnt_oneHundred != 0 && cnt_fifty == 0 && cnt_twentyFive != 0) {
                if (cnt_oneHundred * 3 <= cnt_twentyFive) {
                    res = RESULT_YES;
                } else {
                    res = RESULT_NO;
                }
            } else if (cnt_oneHundred != 0 && cnt_fifty != 0 && cnt_twentyFive != 0) {
                if (cnt_oneHundred <= cnt_fifty && cnt_fifty * 2 <= cnt_twentyFive) {
                    res = RESULT_YES;
                } else if (cnt_oneHundred > cnt_fifty && (cnt_oneHundred - cnt_fifty) * 3 <= cnt_twentyFive - cnt_fifty * 2) {
                    res = RESULT_YES;
                } else {
                    res = RESULT_NO;
                }
            } else {
                res = RESULT_NO;
            }
    
            System.out.println(" cnt_oneHundred = " + cnt_oneHundred + ",
     cnt_fifty = " + cnt_fifty + ",
     cnt_twentyFive = " + cnt_twentyFive + ",
     RESULT = " + res + "
       This Tickets" + lineNo + " is over! 
    ");
            return res;
        }
    
    
    
        public static void main(String[] args) {
            // test
            Tickets(new int[]{25, 25, 25, 25}, 1); // lineNo.1: only have 25: YES
            Tickets(new int[]{25, 25, 50, 50, 25, 50}, 2); // lineNo.2: only have 25 50: cnt_fifty <= cnt_twentyFive YES
            Tickets(new int[]{100, 100, 25, 25, 25, 25, 25, 25}, 3); // lineNo.3: only have 25 100: cnt_oneHundred * 3 <= cnt_twentyFive YES
            Tickets(new int[]{100, 50, 50, 25, 25, 25, 25, 25}, 4); // lineNo.4: have 25 50 100: cnt_oneHundred  <= cnt_fifty && cnt_fifty * 2 <= cnt_twentyFive YES
            Tickets(new int[]{100, 100, 100, 50, 50, 25, 25, 25, 25, 25, 25, 25}, 5); // lineNo.5: have 25 50 100: cnt_oneHundred > cnt_fifty && (cnt_oneHundred - cnt_fifty) * 3 <= cnt_twentyFive - cnt_fifty * 2 YES
            Tickets(new int[]{100, 50, 50, 25, 25}, 6); // lineNo.6: NO
        }
    

      结果:

    cnt_oneHundred = 0,
     cnt_fifty = 0,
     cnt_twentyFive = 4,
     RESULT = YES
       This Tickets1 is over! 
    
     cnt_oneHundred = 0,
     cnt_fifty = 3,
     cnt_twentyFive = 3,
     RESULT = YES
       This Tickets2 is over! 
    
     cnt_oneHundred = 2,
     cnt_fifty = 0,
     cnt_twentyFive = 6,
     RESULT = YES
       This Tickets3 is over! 
    
     cnt_oneHundred = 1,
     cnt_fifty = 2,
     cnt_twentyFive = 5,
     RESULT = YES
       This Tickets4 is over! 
    
     cnt_oneHundred = 3,
     cnt_fifty = 2,
     cnt_twentyFive = 7,
     RESULT = YES
       This Tickets5 is over! 
    
     cnt_oneHundred = 1,
     cnt_fifty = 2,
     cnt_twentyFive = 2,
     RESULT = NO
       This Tickets6 is over! 
    

      

  • 相关阅读:
    冒泡排序及优化
    Map的三种遍历
    抽象类以及接口的异同
    安卓仿制新浪微博(一)之OAuth2授权接口
    安卓handler.post问题
    Git——版本控制器概述
    Linux概述及简单命令
    JBoss7配置-支持IPv4和IPv6双栈环境
    作用域public,private,protected,以及不写时的区别
    UML类图画法及类之间几种关系
  • 原文地址:https://www.cnblogs.com/bors/p/tickets.html
Copyright © 2011-2022 走看看