[CSP-J 2024] 扑克牌
2024年12月25日大约 1 分钟
【100 分】set
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
set<string> se;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> s;
se.insert(s);
}
cout << 52 - se.size();
return 0;
}
【100 分】直接调用 ascii 统计
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
string t1 = "DCHS";
string t2 = "A23456789TJQK";
int cnt[256][256];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> s;
cnt[s[0]][s[1]]++;
}
int ans = 52;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 13; j++)
if (cnt[t1[i]][t2[j]])
ans--;
cout << ans;
return 0;
}
【100 分】排序去重
#include <bits/stdc++.h>
using namespace std;
int n;
string s[55];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> s[i];
sort(s + 1, s + n + 1);
int cnt = (n >= 1);
for (int i = 2; i <= n; i++)
if (s[i] != s[i - 1])
cnt++;
cout << 52 - cnt;
return 0;
}
【100 分】拿个数组完成字符对数字的映射
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
string t1 = "DCHS";
string t2 = "A23456789TJQK";
int c2i[256]; // char to int
int cnt[5][15]; // 1~4 花色 1~13数字
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
for (int i = 0; i <= 3; i++)
c2i[t1[i]] = i + 1;
for (int i = 0; i <= 12; i++)
c2i[t2[i]] = i + 1;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> s;
cnt[c2i[s[0]]][c2i[s[1]]]++;
}
int ans = 52;
for (int i = 1; i <= 4; i++)
for (int j = 1; j <= 13; j++)
if (cnt[i][j])
ans--;
cout << ans;
return 0;
}