解题思路
-
牌型规则(从大到小):
- 豹子:三张相同的牌
- 顺子:三张连续的牌
- 对子:两张相同的牌
- 普通牌:比较最大牌
-
解题步骤:
- 解析输入字符串,转换为数字(2-14,其中J=11,Q=12,K=13,A=14)
- 对每个玩家的牌排序
- 判断牌型
- 按规则比较大小
代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 将字符串转换为牌值数组
vector<int> parseCards(string& s) {
vector<int> cards;
for(int i = 0; i < s.length(); i++) {
if(s[i] >= '2' && s[i] <= '9') {
cards.push_back(s[i] - '0');
} else if(s[i] == '1') {
if(i + 1 < s.length() && s[i + 1] == '0') {
cards.push_back(10);
i++;
} else {
return vector<int>(); // 非法输入
}
} else if(s[i] == 'J') cards.push_back(11);
else if(s[i] == 'Q') cards.push_back(12);
else if(s[i] == 'K') cards.push_back(13);
else if(s[i] == 'A') cards.push_back(14);
else return vector<int>(); // 非法输入
}
sort(cards.begin(), cards.end());
return cards;
}
// 获取牌型(4=豹子,3=顺子,2=对子,1=普通牌)
pair<int, pair<int,int>> getType(vector<int>& cards) {
if(cards[0] == cards[1] && cards[1] == cards[2]) {
return {4, {cards[0], 0}};
}
if(cards[2] - cards[1] == 1 && cards[1] - cards[0] == 1) {
return {3, {cards[0], 0}};
}
if(cards[0] == cards[1]) {
return {2, {cards[0], cards[2]}};
}
if(cards[1] == cards[2]) {
return {2, {cards[1], cards[0]}};
}
return {1, {0, 0}};
}
int compare(vector<int>& a, vector<int>& b) {
if(a.empty() || b.empty()) return -2; // 非法输入
auto typeA = getType(a);
auto typeB = getType(b);
if(typeA.first > typeB.first) return 1;
if(typeA.first < typeB.first) return -1;
// 同牌型比较
if(typeA.first == 4 || typeA.first == 3) {
if(a[0] > b[0]) return 1;
if(a[0] < b[0]) return -1;
return 0;
}
if(typeA.first == 2) {
if(typeA.second.first > typeB.second.first) return 1;
if(typeA.second.first < typeB.second.first) return -1;
if(typeA.second.second > typeB.second.second) return 1;
if(typeA.second.second < typeB.second.second) return -1;
return 0;
}
// 普通牌从大到小比较
for(int i = 2; i >= 0; i--) {
if(a[i] > b[i]) return 1;
if(a[i] < b[i]) return -1;
}
return 0;
}
int main() {
string s1, s2;
while(cin >> s1 >> s2) {
vector<int> cards1 = parseCards(s1);
vector<int> cards2 = parseCards(s2);
cout << compare(cards1, cards2) << endl;
}
return 0;
}
import java.util.*;
public class Main {
// 将字符串转换为牌值数组
static List<Integer> parseCards(String s) {
List<Integer> cards = new ArrayList<>();
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(c >= '2' && c <= '9') {
cards.add(c - '0');
} else if(c == '1') {
if(i + 1 < s.length() && s.charAt(i + 1) == '0') {
cards.add(10);
i++;
} else {
return new ArrayList<>(); // 非法输入
}
} else if(c == 'J') cards.add(11);
else if(c == 'Q') cards.add(12);
else if(c == 'K') cards.add(13);
else if(c == 'A') cards.add(14);
else return new ArrayList<>(); // 非法输入
}
Collections.sort(cards);
return cards;
}
static class CardType {
int type;
int pair;
int other;
CardType(int type, int pair, int other) {
this.type = type;
this.pair = pair;
this.other = other;
}
}
static CardType getType(List<Integer> cards) {
if(cards.get(0).equals(cards.get(1)) && cards.get(1).equals(cards.get(2))) {
return new CardType(4, cards.get(0), 0);
}
if(cards.get(2) - cards.get(1) == 1 && cards.get(1) - cards.get(0) == 1) {
return new CardType(3, cards.get(0), 0);
}
if(cards.get(0).equals(cards.get(1))) {
return new CardType(2, cards.get(0), cards.get(2));
}
if(cards.get(1).equals(cards.get(2))) {
return new CardType(2, cards.get(1), cards.get(0));
}
return new CardType(1, 0, 0);
}
static int compare(List<Integer> a, List<Integer> b) {
if(a.isEmpty() || b.isEmpty()) return -2;
CardType typeA = getType(a);
CardType typeB = getType(b);
if(typeA.type != typeB.type) {
return typeA.type > typeB.type ? 1 : -1;
}
if(typeA.type == 4 || typeA.type == 3) {
if(a.get(0) > b.get(0)) return 1;
if(a.get(0) < b.get(0)) return -1;
return 0;
}
if(typeA.type == 2) {
if(typeA.pair != typeB.pair) return typeA.pair > typeB.pair ? 1 : -1;
if(typeA.other != typeB.other) return typeA.other > typeB.other ? 1 : -1;
return 0;
}
for(int i = 2; i >= 0; i--) {
if(!a.get(i).equals(b.get(i))) {
return a.get(i) > b.get(i) ? 1 : -1;
}
}
return 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String s1 = sc.next();
String s2 = sc.next();
List<Integer> cards1 = parseCards(s1);
List<Integer> cards2 = parseCards(s2);
System.out.println(compare(cards1, cards2));
}
}
}
def parse_cards(s):
cards = []
i = 0
while i < len(s):
if '2' <= s[i] <= '9':
cards.append(int(s[i]))
elif s[i] == '1':
if i + 1 < len(s) and s[i + 1] == '0':
cards.append(10)
i += 1
else:
return [] # 非法输入
elif s[i] == 'J': cards.append(11)
elif s[i] == 'Q': cards.append(12)
elif s[i] == 'K': cards.append(13)
elif s[i] == 'A': cards.append(14)
else: return [] # 非法输入
i += 1
return sorted(cards)
def get_type(cards):
if cards[0] == cards[1] == cards[2]:
return (4, (cards[0], 0))
if cards[2] - cards[1] == 1 and cards[1] - cards[0] == 1:
return (3, (cards[0], 0))
if cards[0] == cards[1]:
return (2, (cards[0], cards[2]))
if cards[1] == cards[2]:
return (2, (cards[1], cards[0]))
return (1, (0, 0))
def compare(a, b):
if not a or not b: return -2 # 非法输入
type_a = get_type(a)
type_b = get_type(b)
if type_a[0] != type_b[0]:
return 1 if type_a[0] > type_b[0] else -1
if type_a[0] in (3, 4):
if a[0] > b[0]: return 1
if a[0] < b[0]: return -1
return 0
if type_a[0] == 2:
if type_a[1][0] != type_b[1][0]:
return 1 if type_a[1][0] > type_b[1][0] else -1
if type_a[1][1] != type_b[1][1]:
return 1 if type_a[1][1] > type_b[1][1] else -1
return 0
# 普通牌从大到小比较
for i in range(2, -1, -1):
if a[i] != b[i]:
return 1 if a[i] > b[i] else -1
return 0
while True:
try:
s1, s2 = input().split()
cards1 = parse_cards(s1)
cards2 = parse_cards(s2)
print(compare(cards1, cards2))
except EOFError:
break
算法及复杂度
- 算法:排序 + 模拟
- 时间复杂度:
- 每次比较只需处理固定数量的牌
- 空间复杂度:
- 只需要存储固定数量的牌