【題解】ZeroJudge d545: 2. 抽紙牌(poker)

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d545
【解題想法】多鍵值排序

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int N, n, M;
  char c;
  while (cin >> N) {
    vector<pair<int, char> > v;
    for (int i=0; i<N; i++) {
      cin >> c >> n;
      v.push_back({n, c});
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    cin >> M;
    cout << v[M-1].second << " " << v[M-1].first << "\n";
  }
    return 0;
}

Python code (credit: Amy Chou)

N = int(input())
lst = input().split()
card = []
for i in range(len(lst)//2):
    c = lst[2*i]
    n = lst[2*i+1]
    if lst[2*i+1].isdigit():
        n = int(n)
    else:
        if (n == 'A'):
            n = 1
        elif (n == 'J'):
            n = 11
        elif (n == 'Q'):
            n = 12
        elif (n == 'K'):
            n = 13
        else:
            n = 0
    card.append((c, n))

card.sort(key=lambda x: (x[1], x[0]), reverse=True)

M = int(input())
print(card[M-1][0], card[M-1][1])
分享本文 Share with friends