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: vector <string>
Returns: vector <string>
Method signature: vector <string> sortByOdds(vector <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

总的来说此题不难,就是组合数学题

#include <vector>
#include <string>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h> using namespace std; class Lottery {
public:
vector <string> sortByOdds(vector <string>);
void compileRule(string, string&, uint64_t&, uint64_t&, int&, int&);
uint64_t computeOdd(uint64_t, uint64_t, int, int);
vector<string> ascendOrder(uint64_t* ,string[], int);
}; vector <string> Lottery::sortByOdds(vector <string> rules) {
int len=rules.size();
int i;
string names[len];
uint64_t choices[len];
uint64_t blanks[len];
uint64_t odds[len];
int sorted[len];
int unique[len];
vector<string> result;
if(len==0){
return result;
}
for(i=0;i<len;i++){
compileRule(rules.at(i), names[i], choices[i], blanks[i], sorted[i], unique[i]);
}
for(i=0;i<len;i++){
odds[i] = computeOdd(choices[i], blanks[i], sorted[i], unique[i]);
}
result=ascendOrder(odds, names, len);
return result;
} void Lottery::compileRule(string rule, string& name, uint64_t& choice, uint64_t& blank, int& sorted, int&unique){
char *a = (char*)malloc(sizeof(char));
choice = 0;
blank = 0;
//extract name until find an :
string::iterator iter;
for(iter=rule.begin();*iter!=':';iter++){
*a=*iter;
name.append((const char*)a);
}
iter=iter+2;
//extract choice
for(;*iter!=' ';iter++){
choice=choice*10+*iter-48;
}
iter++;
//extract blank
for(;*iter!=' ';iter++){
blank=blank*10+*iter-48;
}
//printf("blank %d\n",blank);
iter++;
if(*iter=='T') sorted=1;
else sorted=0;
iter=iter+2;
if(*iter=='T') unique=1;
else unique=0;
//printf("choice %d, blank %d, sorted %d, unique %d\n",choice,blank, sorted, unique);
}
uint64_t Lottery::computeOdd(uint64_t choice, uint64_t blank, int sorted, int unique){
int i;
uint64_t result=1;
uint64_t tresult=1;
if(sorted == 0 && unique == 0){
for(i = 0;i<blank;i++){
result=result*choice;
}
}
if(sorted==1 && unique==0){
for(i = 0;i<blank;i++){
result=result*choice;
}
for(i=0;i<blank;i++){
tresult=tresult*(choice-i);
}
for(i=0;i<blank;i++){
tresult=tresult/(i+1);
}
result=result-tresult;
}
if(sorted==0 && unique==1){
for(i=0;i<blank;i++){
result=result*(choice-i);
}
}
if(sorted==1 && unique==1){
for(i=0;i<blank;i++){
result=result*(choice-i);
}
for(i=0;i<blank;i++){
result=result/(i+1);
}
}
printf("odd %d\n",result);
//printf("choice %d, blank %d, sorted %d, unique %d, odd %d\n",choice,blank, sorted, unique,result);
return result;
} vector<string> Lottery::ascendOrder(uint64_t* odd, string* names, int len){
uint64_t temp;
int order[len];
int i,j, torder;
vector<string> result;
for(i=0;i<len;i++)
order[i]=i;
for(i=0;i<len-1;i++){
for(j=0;j<len-1-i;j++){
if (odd[j]>odd[j+1]){
temp=odd[j];
odd[j]=odd[j+1];
odd[j+1]=temp;
torder=order[j];
order[j]=order[j+1];
order[j+1]=torder;
}
}
}
for(i=0;i<len;i++)
printf("%d ",odd[i]);
for(i=0;i<len;i++)
printf("%d ",order[i]);
for(i=0;i<len;i++){
result.push_back(names[order[i]]);
}
return result;
}

  

topcoder算法练习2的更多相关文章

  1. topcoder算法练习3

    SRM144 DIV1 1100 point Problem Statement      NOTE: There are images in the examples section of this ...

  2. ITWorld:2014年全球最杰出的14位编程天才

    近日,ITWorld 整理全球最杰出的 14 位程序员,一起来看下让我们膜拜的这些大神都有哪些?(排名不分先后) 1.Jon Skeet 个人名望:程序技术问答网站 Stack Overflow 总排 ...

  3. BFS/DFS算法介绍与实现(转)

    广度优先搜索(Breadth-First-Search)和深度优先搜索(Deep-First-Search)是搜索策略中最经常用到的两种方法,特别常用于图的搜索.其中有很多的算法都用到了这两种思想,比 ...

  4. IT求职中,笔试、面试的算法准备

    PS:此文章为转载,源地址:http://www.newsmth.net/nForum/#!article/CoderInterview/849     作者应该是在美国进行的笔试面试,感觉面试的的公 ...

  5. *[topcoder]LCMSetEasy

    http://community.topcoder.com/stat?c=problem_statement&pm=13040 DFS集合全排列+LCM和GCD.但事实上,有更简单的算法,列在 ...

  6. *[topcoder]LittleElephantAndBalls

    http://community.topcoder.com/stat?c=problem_statement&pm=12758&rd=15704 topcoder的题经常需要找规律,而 ...

  7. [topcoder]KingdomReorganization

    http://community.topcoder.com/stat?c=problem_statement&pm=11282&rd=14724 这道题是最小生成树,但怎么转化是关键. ...

  8. [topcoder]ActivateGame

    http://community.topcoder.com/stat?c=problem_statement&pm=10750&rd=14153 http://apps.topcode ...

  9. [topcoder]BestRoads

    http://community.topcoder.com/stat?c=problem_statement&pm=10172&rd=13515 http://community.to ...

随机推荐

  1. unity3d Human skin real time rendering plus 真实模拟人皮实时渲染 plus篇

    最近逃课做游戏,逃的有几门都要停考了,呵呵呵,百忙之中不忘超炒冷饭,感觉之前的人皮效果还是不够好,又改进了一些东西 首先上图 放大看细节 显而易见的比上次的效果要好很多,此次我把模型用3dmax进行了 ...

  2. Linux给用户增加sudo权限

    有时候我们在Linux下执行sudo的时候,出现 xxx is not int the sudoers file 告诉我们当前用户不是sudoer,所以我们要把当前用户添加进去,步骤如下: 1.进入超 ...

  3. 高性能I/O设计模式Reactor和Proactor

    系统I/O 可分为阻塞型, 非阻塞同步型,非阻塞异步型. (Linux对aio支持的不完整,所以linux上用Reactor比较多:Proactor需要系统API支持真正的“异步”) 阻塞型I/O意味 ...

  4. 《Numerical Methods》-chaper4-一元非线性方程的解

    在许多生产时间问题中,我们根据已知条件往往会列出一个一元非线性方程,一个最典型的例子就是银行存款的问题,由于其利息需要基于前一年的本息和,因此列出来的方程x的指数往往是高次的.还有物理问题当中一系列用 ...

  5. nopcommerce中文网

    nopcommerce中文网 | nopcommerce是国外asp.net领域一个高质量的b2c开源项目,基于EntityFramework和MVC开发,交流QQ群:75272942 nopcomm ...

  6. .\Obj\main.axf: Error: L6406E: No space in execution regions with .ANY selector matching sin_i.o(.co

    这个问题原因是 芯片的 空间不足 解决方法是  在KEIL 的DEVICE中选择 更大的空间的芯片型号

  7. 设置UWP程序自启动(Automate launching Windows 10 UWP apps)

    在开发UWP程序的过程中,有时候需要设置程序的自启.本人实现的步骤如下: 1.在VS中激活Protocol (Package.appxmanifest --> Declarations --&g ...

  8. 我的docker 使用笔记

    0 容器启动 docker run image_name(镜像名称) echo "hello word" 1 启动容器 退出后 重新进入 方法一 sudo docker exec ...

  9. [AngularJS + Unit Testing] Testing Directive's controller with bindToController, controllerAs and isolate scope

    <div> <h2>{{vm.userInfo.number}} - {{vm.userInfo.name}}</h2> </div> 'use str ...

  10. [Reactive Programming] Using an event stream of double clicks -- buffer()

    See a practical example of reactive programming in JavaScript and the DOM. Learn how to detect doubl ...