编程练习
题目:LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)…他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子…..LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何。为了方便起见,你可以认为大小王是0。
思路:模拟抽牌过程,注意他抽了5张牌,所以数组长度应该为5,然后就分情况讨论当抽到4张王,3张王,两张王,1张王以及没抽到王时的情景,即分别对应数组中0的个数。
java代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65import java.util.ArrayList;
public class isContinuous {
public boolean isContinuous(int [] numbers) {
if(numbers.length!=5) {
return false;
}
int count=0;
//统计数组中大王小王的总数即0的总数
for(int i=0;i<numbers.length;i++) {
if(numbers[i]==0) {
count++;
}
}
//抽到4张王
if(count==4) {
return true;
}
//抽到3张王
if(count==3) {
ArrayList<Integer> list=new ArrayList<Integer>();
for(int c:numbers) {
list.add(c);
}
list.sort(null);
if(list.get(4)-list.get(3)<=4 && list.get(4)-list.get(3)>=1) {
return true;
}
}
//抽到两张王
if(count==2) {
ArrayList<Integer> list=new ArrayList<Integer>();
for(int c:numbers) {
list.add(c);
}
list.sort(null);
if(list.get(4)-list.get(2)<=4 && list.get(4)-list.get(2)>=2) {
return true;
}
}
//抽到1张王
if(count==1) {
ArrayList<Integer> list=new ArrayList<Integer>();
for(int c:numbers) {
list.add(c);
}
list.sort(null);
if(list.get(4)-list.get(1)==4 || list.get(4)-list.get(1)==3) {
return true;
}
}
//没有抽到王
if(count==0) {
ArrayList<Integer> list=new ArrayList<Integer>();
for(int c:numbers) {
list.add(c);
}
list.sort(null);
if(list.get(4)-list.get(0)==4) {
return true;
}
}
return false;
}
}