<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 閱讀(1206) 評(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ò)電話,中國(guó)最優(yōu)秀的網(wǎng)絡(luò)電話
    主站蜘蛛池模板: 亚洲国产精品成人综合久久久| 久久午夜夜伦鲁鲁片免费无码| 亚洲黄色在线观看网站| 高清在线亚洲精品国产二区| 国产香蕉九九久久精品免费| 美女在线视频观看影院免费天天看 | 国产v片免费播放| 国产92成人精品视频免费 | 亚洲中文字幕久久精品无码喷水 | 亚洲国产日韩视频观看| 久久久久亚洲AV成人无码网站| 亚洲一区二区三区国产精品| 四虎影视在线永久免费观看| 成年女人毛片免费观看97| 8888四色奇米在线观看免费看| 97在线免费视频| 国产高清对白在线观看免费91| 亚洲日韩在线中文字幕综合| 色婷五月综激情亚洲综合| 亚洲毛片一级带毛片基地| 久久久久久亚洲av成人无码国产| 国产亚洲视频在线播放| 亚洲成a人一区二区三区| 国产免费黄色大片| 色播在线永久免费视频| 成人午夜视频免费| 毛片高清视频在线看免费观看| 日本免费一区二区在线观看| 无人在线观看免费高清| 久久免费国产精品一区二区| 黄网站免费在线观看| 国产在线观a免费观看| 男女一边桶一边摸一边脱视频免费 | 亚洲JIZZJIZZ妇女| 亚洲精华国产精华精华液| 亚洲av专区无码观看精品天堂| 亚洲六月丁香六月婷婷蜜芽| 亚洲精品偷拍无码不卡av| 亚洲精品动漫在线| 亚洲娇小性xxxx色| 亚洲国产成人AV在线播放|