<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 閱讀(1210) 評論(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;
    }
    }  回復(fù)  更多評論
      
    <2007年10月>
    30123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910




    常用鏈接

    留言簿(10)

    隨筆分類(95)

    隨筆檔案(97)

    文章檔案(10)

    相冊

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

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

    mess

    搜索

    •  

    最新評論

    閱讀排行榜

    校園夢網(wǎng)網(wǎng)絡(luò)電話,中國最優(yōu)秀的網(wǎng)絡(luò)電話
    主站蜘蛛池模板: 久久久久久毛片免费看| 色天使亚洲综合在线观看| 男男gvh肉在线观看免费| 西西大胆无码视频免费| 久久精品国产亚洲αv忘忧草| 最近中文字幕电影大全免费版| 久久久久亚洲AV片无码下载蜜桃| 中文字幕在线免费看线人| 国产亚洲一区二区手机在线观看| 东方aⅴ免费观看久久av| 亚洲成在人天堂在线| 老司机69精品成免费视频| 亚洲福利视频网址| 97在线线免费观看视频在线观看| 亚洲中文字幕精品久久| 国产午夜影视大全免费观看| 曰批免费视频播放在线看片二| 亚洲日本中文字幕一区二区三区| 国产在线国偷精品免费看| 亚洲国产另类久久久精品| 91精品手机国产免费| 亚洲乱码在线卡一卡二卡新区| 狼友av永久网站免费观看| 免费人妻精品一区二区三区| 亚洲国产精品无码av| 一色屋成人免费精品网站| 亚洲AV无码AV日韩AV网站| 爱情岛论坛网亚洲品质自拍| 久久久久国产精品免费免费不卡| 亚洲伊人久久大香线焦| 日本特黄a级高清免费大片| a免费毛片在线播放| 337p日本欧洲亚洲大胆精品555588 | 亚洲国产精品免费在线观看| 免费人成视频在线| 一级做a爱片特黄在线观看免费看| 亚洲国产一成人久久精品| 免费电影在线观看网站| 国产精品免费一区二区三区| 亚洲欧洲日产专区| 亚洲AⅤ永久无码精品AA|