<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(1205) 評論(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

    搜索

    •  

    最新評論

    閱讀排行榜

    校園夢網網絡電話,中國最優秀的網絡電話
    主站蜘蛛池模板: 亚洲精品国产av成拍色拍| 亚洲精品日韩专区silk| 亚洲AV女人18毛片水真多| 日本人的色道免费网站| 久久久亚洲精品国产| a毛片视频免费观看影院| 亚洲综合精品香蕉久久网| 久久精品免费大片国产大片| 亚洲中文字幕无码日韩| 亚洲人成网亚洲欧洲无码| 亚洲一区二区影院| 免费观看黄色的网站| 久九九精品免费视频| 国产AV无码专区亚洲AV男同| 久久精品成人免费网站| 亚洲AV永久无码精品一百度影院 | 丁香花免费高清视频完整版| 中文字幕亚洲免费无线观看日本| 国产香蕉免费精品视频| 亚洲天然素人无码专区| 免费永久国产在线视频| 国产成人无码免费看片软件| 亚洲精品成人无码中文毛片不卡| 日本亚洲欧洲免费天堂午夜看片女人员| 久久精品国产亚洲精品2020| 久久久久久久91精品免费观看| 风间由美在线亚洲一区| 国产精品亚洲成在人线| 日本最新免费网站| 美女被羞羞网站免费下载| 人人狠狠综合久久亚洲婷婷| 4虎永免费最新永久免费地址| 99亚洲精品卡2卡三卡4卡2卡| 成人亚洲性情网站WWW在线观看 | 男女一边摸一边做爽的免费视频| 久久精品国产亚洲AV果冻传媒| 成人免费看吃奶视频网站| 免费视频成人国产精品网站 | 亚洲熟妇无码一区二区三区导航 | 好男人资源在线WWW免费| 亚洲福利秒拍一区二区|