posts - 97,  comments - 93,  trackbacks - 0
          Problem Statement

          In most states, gamblers can choose from a wide variety of different lottery games. The rules of a lottery are defined by two integers (choices and blanks) and two boolean variables (sorted and unique). choices represents the highest valid number that you may use on your lottery ticket. (All integers between 1 and choices, inclusive, are valid and can appear on your ticket.) blanks represents the number of spots on your ticket where numbers can be written.
          The sorted and unique variables indicate restrictions on the tickets you can create. If sorted is set to true, then the numbers on your ticket must be written in non-descending order. If sorted is set to false, then the numbers may be written in any order. Likewise, if unique is set to true, then each number you write on your ticket must be distinct. If unique is set to false, then repeats are allowed.
          Here are some example lottery tickets, where choices = 15 and blanks = 4:
          {3, 7, 12, 14} -- this ticket is unconditionally valid.
          {13, 4, 1, 9} -- because the numbers are not in nondescending order, this ticket is valid only if sorted = false.
          {8, 8, 8, 15} -- because there are repeated numbers, this ticket is valid only if unique = false.
          {11, 6, 2, 6} -- this ticket is valid only if sorted = false and unique = false.
          Given a list of lotteries and their corresponding rules, return a list of lottery names sorted by how easy they are to win. The probability that you will win a lottery is equal to (1 / (number of valid lottery tickets for that game)). The easiest lottery to win should appear at the front of the list. Ties should be broken alphabetically (see example 1).
          Definition
          ????
          Class:
          Lottery
          Method:
          sortByOdds
          Parameters:
          String[]
          Returns:
          String[]
          Method signature:
          String[] sortByOdds(String[] rules)
          (be sure your method is public)

          Constraints
          -
          rules will contain between 0 and 50 elements, inclusive.
          -
          Each element of rules will contain between 11 and 50 characters, inclusive.
          -
          Each element of rules will be in the format "<NAME>:_<CHOICES>_<BLANKS>_<SORTED>_<UNIQUE>" (quotes for clarity). The underscore character represents exactly one space. The string will have no leading or trailing spaces.
          -
          <NAME> will contain between 1 and 40 characters, inclusive, and will consist of only uppercase letters ('A'-'Z') and spaces (' '), with no leading or trailing spaces.
          -
          <CHOICES> will be an integer between 10 and 100, inclusive, with no leading zeroes.
          -
          <BLANKS> will be an integer between 1 and 8, inclusive, with no leading zeroes.
          -
          <SORTED> will be either 'T' (true) or 'F' (false).
          -
          <UNIQUE> will be either 'T' (true) or 'F' (false).
          -
          No two elements in rules will have the same name.
          Examples
          0)

          {"PICK ANY TWO: 10 2 F F"
          ,"PICK TWO IN ORDER: 10 2 T F"
          ,"PICK TWO DIFFERENT: 10 2 F T"
          ,"PICK TWO LIMITED: 10 2 T T"}
          Returns:
          { "PICK TWO LIMITED",
            "PICK TWO IN ORDER",
            "PICK TWO DIFFERENT",
            "PICK ANY TWO" }
          The "PICK ANY TWO" game lets either blank be a number from 1 to 10. Therefore, there are 10 * 10 = 100 possible tickets, and your odds of winning are 1/100.
          The "PICK TWO IN ORDER" game means that the first number cannot be greater than the second number. This eliminates 45 possible tickets, leaving us with 55 valid ones. The odds of winning are 1/55.
          The "PICK TWO DIFFERENT" game only disallows tickets where the first and second numbers are the same. There are 10 such tickets, leaving the odds of winning at 1/90.
          Finally, the "PICK TWO LIMITED" game disallows an additional 10 tickets from the 45 disallowed in "PICK TWO IN ORDER". The odds of winning this game are 1/45.
          1)

          {"INDIGO: 93 8 T F",
           "ORANGE: 29 8 F T",
           "VIOLET: 76 6 F F",
           "BLUE: 100 8 T T",
           "RED: 99 8 T T",
           "GREEN: 78 6 F T",
           "YELLOW: 75 6 F F"}
          Returns: { "RED",  "ORANGE",  "YELLOW",  "GREEN",  "BLUE",  "INDIGO",  "VIOLET" }
          Note that INDIGO and BLUE both have the exact same odds (1/186087894300). BLUE is listed first because it comes before INDIGO alphabetically.
          2)


          {}
          Returns: { }
          Empty case
          This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

           1 import java.util.Arrays;
           2 import java.util.Scanner;
           3 
           4 public class Lottery {
           5 
           6     private static class Ticket implements Comparable<Ticket> {
           7 
           8         private long way;
           9         private boolean sort;
          10         private boolean unique;
          11         private int choice;
          12         private int blank;
          13         private String name;
          14 
          15         public Ticket(String s) {
          16             int x;
          17             x = s.indexOf(":");
          18             name = s.substring(0, x);
          19             Scanner in = new Scanner(s.substring(x + 1));
          20             choice = in.nextInt();
          21             blank = in.nextInt();
          22             sort = "T".equals(in.next());
          23             unique = "T".equals(in.next());
          24             calc();
          25         }
          26 
          27         public int compareTo(Ticket o) {
          28             if (way == o.way) {
          29                 return name.compareTo(o.name);
          30             }
          31             if (way < o.way) {
          32                 return -1;
          33             }
          34             return 1;
          35         }
          36 
          37         private void calc() {
          38             int i;
          39             if (sort && unique) {
          40                 way = C(choice, blank);
          41             } else if (sort) {
          42                 way = C(choice - 1 + blank, blank);
          43             } else if (unique) {
          44                 way = 1;
          45                 for (int j = 0; j < blank; j++) {
          46                     way = way * (choice - j);
          47                 }
          48             } else {
          49                 way = 1;
          50                 for (int j = 0; j < blank; j++) {
          51                     way = way * choice;
          52                 }
          53             }
          54         }
          55     }
          56     private static long[][] C;
          57 
          58     private static long C(int choice, int blank) {
          59         if (C[choice][blank] > 0) {
          60             return C[choice][blank];
          61         }
          62         if (blank == 0 || choice == blank) {
          63             return C[choice][blank] = 1;
          64         }
          65         return C[choice][blank] = C(choice - 1, blank - 1+ C(choice - 1, blank);
          66     }
          67     private int n;
          68     private Ticket[] v;
          69 
          70     public String[] sortByOdds(String[] rules) {
          71         C = new long[120][120];
          72         n = rules.length;
          73         v = new Ticket[n];
          74         int i;
          75         int j;
          76         int k;
          77         for (i = 0; i < n; i++) {
          78             v[i] = new Ticket(rules[i]);
          79         }
          80         Arrays.sort(v);
          81         String[] r = new String[n];
          82         for (i = 0; i < n; i++) {
          83             r[i] = v[i].name;
          84         }
          85         return r;
          86     }
          87 }


          posted on 2007-10-24 21:20 wqwqwqwqwq 閱讀(1208) 評論(1)  編輯  收藏 所屬分類: Data Structure && Algorithm

          FeedBack:
          # re: Lottery Again
          2007-10-24 21:22 | 曲強 Nicky
          import java.util.Arrays;
          import java.util.HashMap;

          /*
          Author Nicky Qu
          All Rights Reserved. Oct.24th,2007.
          */
          public class Lottery {

          private String[] temp = new String[4];
          private long[] NotsortedResult;
          private String[] result;
          private HashMap<Long, String> tempHashMap = new HashMap<Long, String>();

          public String[] sortByOdds(String[] rules) {
          String itemName = "";
          boolean boo = rules.length == 1 && rules[0].equals("");
          if (rules.length > 0) {
          if (boo) {
          return rules;
          }
          result = new String[rules.length];
          NotsortedResult = new long[rules.length];
          } else {
          return rules;
          }
          for (int i = 0; i < rules.length; i++) {
          temp = rules[i].substring(rules[i].lastIndexOf(":") + 1).trim().split(" ");
          itemName = rules[i].substring(0, rules[i].lastIndexOf(":"));
          String judgement = temp[2].trim() + temp[3].trim();
          long num = 1;
          long numTF = 1;
          if (judgement.equals("TT")) {
          // SORTED && DIFFERENT
          for (int j = 0; j < Integer.parseInt(temp[1].trim()); j++) {
          num = num * (Integer.parseInt(temp[0].trim()) - j);
          }
          num /= 2;
          } else if (judgement.equals("FF")) {
          //ANY TWO
          for (int j = 0; j < Integer.parseInt(temp[1].trim()); j++) {
          num = num * (Integer.parseInt(temp[0].trim()));
          }
          } else if (judgement.equals("TF")) {
          // SORTED but UNIQUE is not essential
          for (int j = 0; j < Integer.parseInt(temp[1].trim()); j++) {
          num = num * (Integer.parseInt(temp[0].trim())-j);
          numTF = numTF * (Integer.parseInt(temp[0].trim()));
          }
          num /= 2;
          num = numTF - num;
          } else if (judgement.equals("FT")) {
          // UNIQUE but SORTED is not essential
          for (int j = 0; j < Integer.parseInt(temp[1].trim()); j++) {
          num = num * (Integer.parseInt(temp[0].trim()) - j);
          }
          } else {
          return result = new String[]{"There is something wrong occuring!"};
          }
          NotsortedResult[i] = num;
          tempHashMap.put(num, itemName);
          }
          Arrays.sort(NotsortedResult); //the less the more possible to win
          for (long a : NotsortedResult) {
          System.out.println(a);
          }
          for (int i = 0; i < NotsortedResult.length; i++) {
          result[i] = tempHashMap.get(NotsortedResult[i]);
          }
          return result;
          }
          }  回復  更多評論
            
          <2007年10月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910




          常用鏈接

          留言簿(10)

          隨筆分類(95)

          隨筆檔案(97)

          文章檔案(10)

          相冊

          J2ME技術網站

          java技術相關

          mess

          搜索

          •  

          最新評論

          閱讀排行榜

          校園夢網網絡電話,中國最優秀的網絡電話
          主站蜘蛛池模板: 东丽区| 海伦市| 高阳县| 万载县| 南京市| 章丘市| 山西省| 周宁县| 彰化市| 东光县| 顺平县| 延长县| 灵武市| 迁西县| 噶尔县| 绵阳市| 九江县| 乡宁县| 临洮县| 城口县| 龙山县| 关岭| 西林县| 都昌县| 青海省| 珠海市| 潞城市| 扶沟县| 威信县| 龙海市| 延边| 宁陵县| 炉霍县| 竹山县| 东安县| 湟源县| 大关县| 六盘水市| 华坪县| 彭山县| 石门县|