[CSP-J 2024 T1] 扑克牌
第一种方法,模拟算法
#include <bits/stdc++.h>
using namespace std;
int poker[4][15];
//D-0,C-1,H-2,S-3
//A-1,T-10,J-11,Q-12,K-13
int main()
{
int n, cnt = 0;
int huase, dianshu;
char ch1, ch2;
cin >> n;
while (n--)
{
cin >> ch1 >> ch2;
if (ch1 == 'D')
huase = 0;
else if (ch1 == 'C')
huase = 1;
else if (ch1 == 'H')
huase = 2;
else if (ch1 == 'S')
huase = 3;
if (ch2 == 'A')
dianshu = 1;
else if (ch2 == 'T')
dianshu = 10;
else if (ch2 == 'J')
dianshu = 11;
else if (ch2 == 'Q')
dianshu = 12;
else if (ch2 == 'K')
dianshu = 13;
else
dianshu = ch2 - '0';
poker[huase][dianshu] = 1;
}
for (int i = 0; i < 4; i++)
{
for (int j = 1; j <= 13; j++)
{
if (poker[i][j] == 1)
cnt++;
}
}
cout << 52 - cnt << endl;
return 0;
}第二种方法,使用STL中的set。读入的字符串每个都代表一张合法的扑克牌,从而可以使用 C++ STL 中的 set(集合)完成本题。这是因为,set 可以自动去重,去除重复的牌(字符串)后,剩下的字符串就是实际拥有的不同的牌。而一副扑克牌有 52 张牌,使用 52 减去该集合的大小即可求出答案。
#include <bits/stdc++.h>
using namespace std;
int main()
{
set<string> poker;
string t;
int n;
cin >> n;
while (n--)
{
cin >> t;
poker.insert(t);
}
cout << 52 - poker.size() << endl;
return 0;
}