Poker
National Olympiad — Municipal round (2012 season)
Submission name: poker.cpp
Problem summary
Classify five ranks using this priority order: all five equal → Impossible; four equal → Four of a Kind; a triple and pair → Full House; five consecutive distinct ranks → Straight; a triple → Three of a Kind; two pairs → Two Pairs; one pair → One Pair; otherwise Nothing.
Input
Five integer ranks.
Output
Exactly one of the specified messages.
Constraints
Each rank is an integer from 1 through 13.
Worked example
Official statement sample.
Input
1 3 9 3 2
Output
One Pair
Solution analysis
- Count occurrences of every rank and sort the nonzero multiplicities in decreasing order. The multiplicity patterns immediately identify five equal, four equal, a full house, a triple, and pairs.
- A straight additionally requires five distinct ranks and maximum minus minimum equal to four. This is ordinary integer consecutiveness; the statement gives no ace wraparound rule.
- For 1,3,9,3,2 the multiplicities are 2,1,1,1, so the result is One Pair. For 1,1,4,1,4 they are 3,2, giving Full House.
Why it works
The frequency pattern describes every equality relation among the five numbers. Checking the listed cases in their required order and using the exact distinct-consecutive criterion therefore returns precisely the requested first matching classification.
Watch out. Spelling and capitalization are part of the output. Five equal is Impossible rather than Four of a Kind.
Commented C++17 solution
An independently written complete program with standard headers.
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main(){
int count[14]={};
int low=14,high=0;
for(int i=0;i<5;++i){
int x;
cin>>x;
++count[x];
low=min(low,x);
high=max(high,x);
}
vector<int>frequency;
for(int x=1;x<=13;++x){
if(count[x]){
frequency.push_back(count[x]);
}
}
// Multiplicity patterns determine every equality-based category.
sort(frequency.rbegin(),frequency.rend());
string answer;
if(frequency[0]==5){
answer="Impossible";
}
else if(frequency[0]==4){
answer="Four of a Kind";
}
else if(frequency[0]==3&&frequency[1]==2){
answer="Full House";
}
else if(frequency.size()==5&&high-low==4){
answer="Straight";
}
else if(frequency[0]==3){
answer="Three of a Kind";
}
else if(frequency[0]==2&&frequency[1]==2){
answer="Two Pairs";
}
else if(frequency[0]==2){
answer="One Pair";
}
else{
answer="Nothing";
}
cout<<answer<<'\n';
}