<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 閱讀(1212) 評(píng)論(1)  編輯  收藏 所屬分類: Data Structure && Algorithm

    FeedBack:
    # re: Lottery Again
    2007-10-24 21:22 | 曲強(qiáng) 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;
    }
    }  回復(fù)  更多評(píng)論
      
    <2007年10月>
    30123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910




    常用鏈接

    留言簿(10)

    隨筆分類(95)

    隨筆檔案(97)

    文章檔案(10)

    相冊(cè)

    J2ME技術(shù)網(wǎng)站

    java技術(shù)相關(guān)

    mess

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    校園夢(mèng)網(wǎng)網(wǎng)絡(luò)電話,中國最優(yōu)秀的網(wǎng)絡(luò)電話
    主站蜘蛛池模板: 亚洲av无码兔费综合| 久久精品亚洲一区二区三区浴池 | 亚洲乱码中文字幕小综合| 亚洲 欧洲 视频 伦小说| 人人鲁免费播放视频人人香蕉| 秋霞人成在线观看免费视频| 亚洲综合无码精品一区二区三区| 97se亚洲国产综合自在线| 91麻豆最新在线人成免费观看 | 少妇人妻偷人精品免费视频| 亚洲AV无码一区二区二三区入口| 中文字幕日本人妻久久久免费| 午夜影视在线免费观看| 亚洲偷自精品三十六区| 在线视频免费观看www动漫| 亚洲人成网站免费播放| 91精品免费不卡在线观看| 亚洲国产一级在线观看| 国产久爱免费精品视频| 久久久久亚洲AV片无码| **俄罗斯毛片免费| 久久亚洲精品国产精品黑人| 中文字幕免费在线观看| 亚洲色精品三区二区一区| 免费一看一级毛片| 成全高清在线观看免费| 亚洲福利电影在线观看| 国产色婷婷精品免费视频| 五月天婷婷精品免费视频| 亚洲AV无码一区二区二三区软件| 日本在线高清免费爱做网站| 亚洲AV成人片色在线观看高潮| 每天更新的免费av片在线观看| 一区二区亚洲精品精华液| 国产精品免费小视频| 伊人免费在线观看高清版| 亚洲国产夜色在线观看| 亚洲成人高清在线| 日韩精品免费一线在线观看| 国产a级特黄的片子视频免费| 亚洲国产精品自在自线观看|