题目1355:扑克牌顺子
-
题目描述:
-
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。
-
输入:
-
输入有多组数据。
每组数据包含两行,第一行输入一个正数n(0<=n<=14),表示从扑克牌中抽出的扑克牌数。接下来的一行输入n个数,表示从这幅扑克牌中抽出的牌。如果n=0,则结束输入。
-
输出:
-
对应每组数据,如果抽出的牌是顺子,则输出“So Lucky!”。否则,输出“Oh My God!”。
-
样例输入:
-
5 3 5 1 0 4 5 3 5 4 7 6 5 3 5 7 4 8 0
-
样例输出:
-
So Lucky! So Lucky! Oh My God!
算法分析
统计每个扑克牌出现的次数,
1.如果非大小王 也就是非0的数字 出现了两次,肯定组成不了顺子
if(count[i]>1){
printf("Oh My God!\n");
return;
}
2.每张非王的牌最多出现了一次,在这种条件下,查找最大的牌last和最小的非零牌first,以及非零牌个数npai
if(count[i]){
if(!first)
first = i;
last = i;
npai++;
}
只要 first + 1 - last - npai 小于等于 大小王的牌数,那么就可以组成顺子。
意思就是 不连续牌中的空隙可以被大小王牌填充的话,就可以组成顺子。
源程序
//============================================================================
// Name : judo1355.cpp
// Author : wdy
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <stdio.h>
const int N = 15;
int count[N]={0};
using namespace std;
void judge(int n){
for(int i = 0;i<N;i++)
count[i] = 0;
int tem=0;
for(int i =0;i<n;i++){
scanf("%d",&tem);
count[tem]++;
}
//find the first non zero
int first = 0;
int last = 0;
int npai = 0;
for(int i = 1;i<N;i++){
if(count[i]>1){
printf("Oh My God!\n");
return;
}
if(count[i]){
if(!first)
first = i;
last = i;
npai++;
}
}
if(last + 1 -first - npai <=count[0])
printf("So Lucky!\n");
else
printf("Oh My God!\n");
}
void judo(){
int n = 0;
while(scanf("%d", &n) != EOF && n!=0){
judge(n);
//std::cout<<n;
}
}
int main() {
judo();
return 0;
}
/**************************************************************
Problem: 1355
User: KES
Language: C++
Result: Accepted
Time:10 ms
Memory:1520 kb
****************************************************************/