数组计算缺失的扑克牌 Array – Finding Missing Cards

1.6k 词

题目描述

Taro is going to play a card game. However, now he has only n cards, even though there should be 52 cards (he has no Jokers). The 52 cards include 13 ranks of each of the four suits: spade, heart, club and diamond.

输入

In the first line, the number of cards n (n ≤ 52) is given. In the following n lines, data of the n cards are given. Each card is given by a pair of a character and an integer which represent its suit and rank respectively. A suit is represented by ‘S’, ‘H’, ‘C’ and ‘D’ for spades, hearts, clubs and diamonds respectively. A rank is represented by an integer from 1 to 13.

输出

Print the missing cards. The same as the input format, each card should be printed with a character and an integer separated by a space character in a line. Arrange the missing cards in the following priorities: Print cards of spades, hearts, clubs and diamonds in this order. If the ranks are equal, print cards with lower ranks first.

题解

#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
int s[13],h[13],c[13],d[13];
int ss=0,hh=0,cc=0,dd=0;
char t;
cin>>n;
while(n–){
cin>>t;
switch(t){
case ‘S’ :
cin>>s[ss];
ss++;
break;
case ‘H’ :
cin>>h[hh];
hh++;
break;
case ‘C’ :
cin>>c[cc];
cc++;
break;
case ‘D’ :
cin>>d[dd];
dd++;
break;
}
}
sort(s,s+ss);
sort(h,h+hh);
sort(c,c+cc);
sort(d,d+dd);
for (int i = 1, j = 0; i <= 13; i++)
{
if (s[j] != i)
cout << “S “ << i << endl;
else
++j;
}
for (int i = 1, j = 0; i <= 13; i++)
{
if (h[j] != i)
cout << “H “ << i << endl;
else
++j;
}
for (int i = 1, j = 0; i <= 13; i++)
{
if (c[j] != i)
cout << “C “ << i << endl;
else
++j;
}
for (int i = 1, j = 0; i <= 13; i++)
{
if (d[j] != i)
cout << “D “ << i << endl;
else
++j;
}
return 0;
}