首页 > 试题广场 >

扑克牌大小

[编程题]扑克牌大小
  • 热度指数:94616 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

扑克牌游戏大家应该都比较熟悉了,一副牌由54张组成,含3~A、2各4张,小王1张,大王1张。牌面从小到大用如下字符和字符串表示(其中,小写joker表示小王,大写JOKER表示大王):
3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER
输入两手牌,两手牌之间用"-"连接,每手牌的每张牌以空格分隔,"-"两边没有空格,如:4 4 4 4-joker JOKER。
请比较两手牌大小,输出较大的牌,如果不存在比较关系则输出ERROR。
基本规则:
(1)输入每手牌可能是个子、对子、顺子(连续5张)、三个、炸弹(四个)和对王中的一种,不存在其他情况,由输入保证两手牌都是合法的,顺子已经从小到大排列;
(2)除了炸弹和对王可以和所有牌比较之外,其他类型的牌只能跟相同类型的存在比较关系(如,对子跟对子比较,三个跟三个比较),不考虑拆牌情况(如:将对子拆分成个子);
(3)大小规则跟大家平时了解的常见规则相同,个子、对子、三个比较牌面大小;顺子比较最小牌大小;炸弹大于前面所有的牌,炸弹之间比较牌面大小;对王是最大的牌;

(4)输入的两手牌不会出现相等的情况。

数据范围:字符串长度:




输入描述:

输入两手牌,两手牌之间用"-"连接,每手牌的每张牌以空格分隔,"-"两边没有空格,如 4 4 4 4-joker JOKER。



输出描述:

输出两手牌中较大的那手,不含连接符,扑克牌顺序不变,仍以空格隔开;如果不存在比较关系则输出ERROR。

示例1

输入

4 4 4 4-joker JOKER

输出

joker JOKER
叫地主,超级加倍,明牌,飞机!
重新定义牌型的大小,思路清晰

#include "stdio.h"
#include "string.h"
//检测空格个数,判断牌型是否一致
int fun(char *a)
{
    int len=strlen(a);
    int k=0;
    for (int i=0;i<len;i++)
    {
        if (a[i]==' ') k++;
    }
    return k;
    
}
//根据牌型的前一到两个字符,来判断大小。
int trans (char *a)
{
    int k=0;
    if (a[0]>='3' && a[0]<='9') k=a[0]-'0'; //3-9字符,直接输出int
    else if (a[0]=='1') k=10;//首字符为1即代表是 10  
    else if (a[0]=='J' && a[1]==' ') k=11; //J-大王 赋予新的int值
    else if (a[0]=='Q') k=12;
    else if (a[0]=='K') k=13;
    else if (a[0]=='A') k=14;
    else if (a[0]=='2') k=15;
    else if (a[0]=='j') k=16;
    else if (a[0]=='J' && a[1]=='O') k=17;
    return k;
}

int main ()
{
    char str[1000]={0};
    
    
    while(gets(str)!=NULL)
    {
        char sta[1000]={0};
        char stb[1000]={0};
        int len=strlen(str);
        int i,k;
        for (i=0;i<len;i++)
        {
            if (str[i]=='-') 
            {
                k=i; break;
            }
        }
        memcpy(sta,str,k); //获得第一手牌
        sta[k]='\0';
        memcpy(stb,str+k+1,len-k-1);  //获得第二首牌
        stb[len-k-1]='\0';
        if ((sta[0]=='j' && sta[6]=='J') || (stb[0]=='j' && stb[6]=='J'))
            printf("joker JOKER\r\n");//先考虑王炸的情况,只要有王炸直接输出王炸
        else  //没有王炸
        {
        int spa=fun(sta);
        int spb=fun(stb);
             if (spa==spb)  //且牌型一致
             {
                 if (trans(sta)>trans(stb)) printf("%s\r\n",sta); //只需比较返回的int型即可比较大小
                 else printf("%s\r\n",stb);
             }
             else  //牌型不一致
             {
                 if (spa==3)printf("%s\r\n",sta); //是否第一手牌有**?
                 else if (spb==3)printf("%s\r\n",stb);//那第二手牌呢?
                 else printf("ERROR\r\n"); //都没有**就输出错误

             }
        }
    }
}


发表于 2020-09-12 13:53:13 回复(0)
#include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<string> split(string str, const char c){//字符串分割函数
    vector<string> sstr;
    int pos = 0;
    while (pos < str.length()){
        int end;
        for (end = pos; str[end] != c && end < str.length(); end++);
        sstr.push_back(str.substr(pos, end - pos));
        pos = end + 1;
    }
    return sstr;
}
int main(){
    string inputs;
    vector<int>cards = { 3 + '0', 4 + '0', 5 + '0', 6 + '0', 7 + '0', 8 + '0', 9 + '0', 10 + '0', 'J', 'Q', 'K', 'A', 2 + '0' };
    vector<int> cmp(128, 0);
    char c = 10 + '0';//将“10”转成字符c
    for (int i = 1; i < cards.size(); i++){
        cmp[cards[i]] = cmp[cards[i - 1]] + 1;
    }
    
    while (getline(cin, inputs)){
        vector<string> twoCard = split(inputs, '-');
        //考虑对王情况
        if (twoCard[0] == "joker JOKER" || twoCard[1] == "joker JOKER"){
            cout << "joker JOKER" << endl;
            continue;
        }
        else if (twoCard[0] == "JOKER joker" || twoCard[1] == "JOKER joker"){
            cout << "JOKER joker" << endl;
            continue;
        }
        vector<string> firstCard = split(twoCard[0], ' ');
        vector<string> secondCard = split(twoCard[1], ' ');
        //考虑非对王的炸的情况
        if (firstCard.size() == 4 || secondCard.size() == 4){
            if (firstCard.size() != 4){
                cout << twoCard[1] << endl;
                continue;
            } else if(secondCard.size() != 4){
                cout << twoCard[0] << endl;
                continue;
            }
            else{//两遍都是炸
                firstCard[0] = firstCard[0] == "10" ? ("" + c) : firstCard[0];//处理10这张牌
                secondCard[0] = secondCard[0] == "10" ? ("" + c) : secondCard[0];//处理10这张牌
                if (cmp[firstCard[0][0]] > cmp[secondCard[1][0]]){
                    cout << twoCard[0] << endl;//谁的炸大就打印谁的牌
                    continue;
                }
                else{
                    cout << twoCard[1] << endl;
                    continue;
                }
            }
        }
        //考虑单张牌的情况,要考虑单张大小王的情况
        else if (firstCard.size() == 1 && secondCard.size() == 1){
            if (twoCard[0] == "JOKER" || twoCard[1] == "JOKER"){//大王
                cout << "JOKER" << endl;
                continue;
            }
            else if (twoCard[0] == "joker" || twoCard[1] == "joker"){//没有大王有小王
                cout << "joker" << endl;
                continue;
            }
            else{//没有大小王
                firstCard[0] = firstCard[0] == "10" ? ("" + c) : firstCard[0];//处理10这张牌
                secondCard[0] = secondCard[0] == "10" ? ("" + c) : secondCard[0];//处理10这张牌
                if (cmp[firstCard[0][0]] > cmp[secondCard[0][0]]){
                    cout << twoCard[0] << endl;//谁的大就打印谁的牌
                    continue;
                }
                else{
                    cout << twoCard[1] << endl; 
                    continue;
                }
            }        
        }
        else if ((firstCard.size() == 2 && secondCard.size() == 2) || 
            (firstCard.size() == 3 && secondCard.size() == 3) ||
            (firstCard.size() == 5 && secondCard.size() == 5)){//一对数,或者三张或者5张没有大小王,没有炸
            firstCard[0] = firstCard[0] == "10" ? ("" + c) : firstCard[0];//处理第一张是10的这张牌
            secondCard[0] = secondCard[0] == "10" ? ("" + c) : secondCard[0];//处理第一张是10的这张牌
            if (cmp[firstCard[0][0]] > cmp[secondCard[0][0]]){
                cout << twoCard[0] << endl;//谁的大就打印谁的牌
                continue;
            }
            else{
                cout << twoCard[1] << endl;
                continue;
            }
        }
        else{
            cout << "ERROR" << endl;
        }
    }
    return 0;
}

发表于 2019-07-21 15:39:50 回复(1)
因为输入的牌是排好序的,可以进行比较的(不能比较的直接error),所以直接if-else即可
分为以下几种情况:
1.含有joker JOKER就直接输出
2.长度不相等的情况下谁长度为4就输出谁(1情况else后**必赢)
3.长度相等的情况下谁第一张牌最大谁就赢,这里不能使用unicode字符集进行比较,因为joker和JOKER两张牌是字符串
4.长度不相等并且没有排除1和2的情况无法进行比较,输出error

这里需要注意,上面说的长度不是说两副牌的长度,而是说两副牌中的牌的张数,所以使用-分割之后还需要以空格 分割,否则JOKER-J失效
发表于 2017-09-19 13:51:29 回复(0)
package HUAWEI3;

import java.util.Scanner;

public class Demo08 {
	public static void main(String[] args) throws Exception{
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			String s = sc.nextLine();
			compare(s);
		}
		sc.close();
	}
	public static void compare(String s){
		String[] arr;
		arr = s.split("-");
		if(arr.length!=2){
			System.out.println();
			return;
		}
		if(arr[0].contains("joker JOKER")||arr[1].equals("joker JOKER")){//为啥必须用equals
			System.out.println("joker JOKER");
		}
		else{
			if(arr[0].split(" ").length==4&&arr[1].split(" ").length!=4){
				System.out.println(arr[0]);
				return;
			}else if(arr[1].split(" ").length==4&&arr[0].split(" ").length!=4){
				System.out.println(arr[1]);
				return;
			}
			if(arr[0].split(" ").length==arr[1].split(" ").length){
				String num1 = arr[0].split(" ")[0];
				String num2 = arr[1].split(" ")[0];
				int num1_flag = 0;
				int num2_flag = 0;
				String[] arr_biaozhun ={"3","4","5","6","7","8","9","10","J","Q","K","A","2","joker","JOKER"};
				for(int i=0;i<arr_biaozhun.length;i++){
					if(num1.equals(arr_biaozhun[i])){
						num1_flag = i;
					}
					if(num2.equals(arr_biaozhun[i])){
						num2_flag = i;
					}
				}
				if(num1_flag>num2_flag){
					System.out.println(arr[0]);
					return;
				}else if(num2_flag>num1_flag){
					System.out.println(arr[1]);
					return;
				}else{
					System.out.println("ERROR");
					return;
				}
			}else{
				System.out.println("ERROR");
				return;
			}
			
		}
	}
}


发表于 2017-01-11 23:18:30 回复(0)
# 2022/3/12 16:03 - 时间
# PyCharm - 当前集成开发环境
# 思路 : 类型一样和类型不一样的
power = ["3","4","5","6","7","8","9","10","J","Q","K","A","2","joker","JOKER"]
while True:
    try:
        a,b = input().split('-')
        sas = a.split()
        sbs = b.split()
        # 类型一样
        if len(sas) == len(sbs):
            if power.index(sas[0])>power.index(sbs[0]):
                print(a)
            else:
                print(b)
        #类型不一样
        else:
            # 有王炸直接输出王炸
            if a =="joker JOKER"&nbs***bsp;b =="joker JOKER":
                print("joker JOKER")
            # a有炸弹
            elif len(sas)==4:
                print(a)
            # b有炸弹
            elif len(sbs)==4:
                print(b)
            #其他类型,无法比较
            else:
                print("ERROR")
    except:
        break

发表于 2022-03-12 16:17:19 回复(0)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author letere
 * @create 2021-11-05 18:14
 */
public class Main {
    public static Map<String, Integer> poke = getData();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String[] strArr = sc.nextLine().split("-");
            System.out.println(compare(strArr[0], strArr[1]));
        }
    }

    // 比较牌大小,返回大牌
    public static String compare(String str1, String str2) {
        int level1 = getLevel(str1.split(" "));
        int level2 = getLevel(str2.split(" "));
        // 不存在比较关系
        if (level1 / 100 != level2 / 100
                && level1 / 100 != 5
                && level2 / 100 != 5) {
            return "ERROR";
        } else {
        // 存在比较关系,直接比较大小
            if (level1 - level2 > 0) {
                return str1;
            }
            return str2;
        }
    }

    // 计算牌的大小(个子以100开头,对子200,三张300,顺子400,**500,王炸599)
    public static int getLevel(String[] arr) {
        // 个子
        if (arr.length == 1) {
            return 100 + poke.get(arr[0]);
        }
        // 对子(王炸)
        if (arr.length == 2) {
            if (!arr[0].equals("joker") && !arr[0].equals("JOKER")) {
                return 200 + poke.get(arr[0]);
            }
            return 599;
        }
        // 三张
        if (arr.length == 3) {
            return 300 + poke.get(arr[0]);
        }
        // 顺子
        if (arr.length == 5) {
            return 400 + poke.get(arr[0]);
        }
        // **
        if (arr.length == 4) {
            return 500 + poke.get(arr[0]);
        }
        return 0;
    }

    // 获取牌对应的优先级
    public static Map<String, Integer> getData() {
        Map<String, Integer> map = new HashMap<>();
        map.put("3", 3);
        map.put("4", 4);
        map.put("5", 5);
        map.put("6", 6);
        map.put("7", 7);
        map.put("8", 8);
        map.put("9", 9);
        map.put("10", 10);
        map.put("J", 11);
        map.put("Q", 12);
        map.put("K", 13);
        map.put("A", 14);
        map.put("2", 15);
        map.put("joker", 16);
        map.put("JOKER", 17);
        return map;
    }
}

发表于 2021-11-05 18:58:40 回复(0)
这个题存在BUG,用以下代码可以通过所有案例,但是案例没有涉及所有情况。所以以下代码可以正常通过该题,但是不具备完全普遍性。
存在的BUG在第40行,已经写在代码备注的地方,其他部分没有问题。
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()){
            String S = in.nextLine();     // 输入手牌
            String s[] = S.split("-");    // s[0]:左手牌,s[1]:右手牌
            String s1[] = s[0].split(" "), s2[] = s[1].split(" ");  // 拆分出每手牌的牌面,目的是得到每手牌的张数
            int n1 = s1.length, n2 = s2.length;                     // 每手牌的张数
            //******************************************************************** 有王炸的情况
            if(n1==2 || n2==2){
                if(n1==2 && s[0].contains("joker") && s[0].contains("JOKER")){  // 左手牌有王炸,直接输出
                    System.out.println(s[0]);
                    continue;
                }
                if(n2==2 && s[1].contains("joker") && s[1].contains("JOKER")){  // 右手牌有王炸,直接输出
                    System.out.println(s[1]);
                    continue;
                }
            }
            //*************************************************************** 有4连炸的情况
            if(n1==4 || n2==4){
                if(n1 == n2){                                              // 两手牌都是**,输出大的那一个
                    String t = s[0].charAt(0)>s[1].charAt(0) ? s[0]:s[1];
                    System.out.println(t);
                    continue;
                }else if(n1 == 4){              // 左手牌有**,直接输出
                    System.out.println(s[0]);
                    continue;
                }else if(n2 == 4){              // 右手牌有**,直接输出
                    System.out.println(s[1]);
                    continue;
                }
            }
            //*************************************************************** 非**的情况
            if(n1 == n2){              // 同类型才能比较,即每手牌的张数相等
                // !!注意:此处需要比较每手牌的最后一张牌面(即最后一个字符)
                // 若比较第一个字母,则(10 J Q K A)不能正常比较,会用'1'去和其他顺子比较
                // 例(3 4 5 6 7-10 J Q K A),会发生'3'>'1'判定左手牌大,判断错误,所以需要判断最后一个字母
                String t = s[0].charAt(s[0].length()-1)>s[1].charAt(s[1].length()-1) ? s[0]:s[1];
                // !!但是这个也不严谨,对于(3 4 5 6 7-6 7 8 9 10)也会判断错误,不过可以通过该题的所有案例,存在BUG
                System.out.println(t);
            }else{                     // 非同类型不能比较
                System.out.println("ERROR");
            }
        }
    }
}


发表于 2021-08-14 15:30:12 回复(0)
自己写了注释
import java.util.*;
public class Main{
    public static void main(String[] args){
        //本题不考虑拆牌,输入要不是是对子,就是对子,或者顺子。。。,不会有对子加对子这种
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            String[] cards = str.split("-");
            String[] card1 = cards[0].split(" ");
            String[] card2 = cards[1].split(" ");
            int[] card_1 = changeCard(card1);
            int[] card_2 = changeCard(card2);
            
            int result = solve(card_1,card_2);
            if(result == 0){
                  System.out.println("ERROR");
            }else if(result == 1){
                  System.out.println(cards[0]);
            }else{
                 System.out.println(cards[1]);
            }
        }
        
    }
    
    //比较大小  1表示card1大,2表示card2大
    private static int solve(int[] card1,int[] card2){
        int sym1 = check(card1);//手牌的模式
        int sym2 = check(card2);
        
        //输入的两手牌不会出现相等的情况,且已经升序排列
        if(sym1 == sym2){//有这几种情况:两手都是  单只,对子,顺子、三个、**  只要比较最小的牌(最前的牌)
            return card1[0]>card2[0] ? 1:2;
        }else{
            //有这几种情况: 要不不能比较,要不有**或者大小王
            if(sym1 == 6 || sym2 == 6){//有大小王
                return sym1 == 6 ? 1:2; 
            }
            if(sym1 == 4 || sym2 == 4){//有**
                return sym1 == 4 ? 1:2; 
            }
            return 0;//0表示异常,两手牌不能比较
        }
    }
    
    //判断手牌的模式
    private static int check(int[] card){
          int len = card.length;
          if(len == 1){
              return 1;//单张
          }else if(len == 2 && card[0] == 16 && card[1] == 17){
              return 6;//对王
          }else if(len == 2 && card[0] == card[1]){
              return 2;//对子
          }else if(len == 3){
              return 3;//三张
          }else if(len == 5){
              return 5;//顺子
          }else if(len == 4){
              return 4;//**
          }
        return -1;
    }
    
    //改变为纯数字
    private static int[] changeCard(String[] card){
        int[] temp = new int[card.length];
        int index = 0;
        for(String s : card){
            switch(s){
                case "J":
                    temp[index] = 11;
                    break;
                case "Q":
                    temp[index] = 12;
                    break;
                case "K":
                    temp[index] = 13;
                    break;
                case "A":
                    temp[index] = 14;
                    break;
                case "2":
                    temp[index] = 15;
                    break;
                case "joker":
                    temp[index] = 16;
                    break;
                case "JOKER":
                    temp[index] = 17;
                    break;
                default :
                    temp[index] = Integer.parseInt(s);
            }
            index++;
        }
        return temp;
    }
}


发表于 2021-04-08 15:51:47 回复(0)
java 解法
import java.util.*;

public class Main { 
    private static Map<String, Integer> map = new HashMap<String, Integer>() {
        {
            put("3", 3);
            put("4", 4);
            put("5", 5);
            put("6", 6);
            put("7", 7);
            put("8", 8);
            put("9", 9);
            put("10", 10);
            put("J", 11);
            put("Q", 12);
            put("K", 13);
            put("A", 14);
            put("2", 15);
            put("joker", 16);
            put("JOKER", 17);
        }
    };
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        // 如果字符串包含王炸,直接输出,排除这种情况
        if (str.contains("joker JOKER")) {
            System.out.println("joker JOKER");
        } else {
            String[] strs = str.split("-");
            String[] poker1 = strs[0].split(" ");
            String[] poker2 = strs[1].split(" ");
            int len1 = poker1.length;
            int len2 = poker2.length;
            // 同类型的牌对比,通过总和的大小来对比
            if (len1 == len2) {
                int sum1 = 0;
                int sum2 = 0;
                for (String s1 : poker1) {
                    sum1 += map.get(s1);
                }
                for (String s2 : poker2) {
                    sum2 += map.get(s2);
                }
                System.out.println(sum1 > sum2? strs[0] : strs[1]);
            // 不同类型的牌,如果有**输出**,没有则报 ERROR
            } else {
                if (len1 == 4) {
                    System.out.println(strs[0]);
                } else if (len2 == 4) {
                    System.out.println(strs[1]);
                } else {
                    System.out.println("ERROR");
                }
            }
        }
    }
}


发表于 2021-02-22 17:37:31 回复(0)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            String[] jiben = {"3","4","5","6","7","8","9","M","J","Q","K","A","2"};
            Map<String, Integer> sortMap = new HashMap<>();
            for(int i = 0; i<jiben.length; i++){
                sortMap.put(jiben[i], i);
            }

            String ls = scanner.nextLine();
            String lsl = ls;
            String[] pp = lsl.split("-");
            if(ls.contains("10")){
                ls = ls.replace("10","M");
            }
            ls = ls.replaceAll(" ", "");
            String[] p = ls.split("-");
            int lengthP0 = p[0].length();
            int lengthP1 = p[1].length();
            if(p[1].contains("joker")|| p[0].contains("joker")){
                System.out.println("joker JOKER");
            }else if(lengthP1 == lengthP0){
                    if(lengthP0 != 3){
                        if(sortMap.get(p[0].substring(0,1))>sortMap.get(p[1].substring(0,1))){
                            System.out.println(pp[0]);
                        }else {
                            System.out.println(pp[1]);
                        }
                    }else {
                        //找出两手牌三个中最大的比较
                        int p0Max=0;
                        int p1Max=0;
                        for(int i = 1; i<4; i++){
                            p0Max = Math.max(p0Max, sortMap.get(p[0].substring(i-1,i)));
                            p1Max = Math.max(p1Max, sortMap.get(p[1].substring(i-1,i)));
                        }
                        if(p0Max>p1Max){
                            System.out.println(pp[0]);
                        }else {
                            System.out.println(pp[1]);
                        }
                    }
            }else {
                if(p[0].length()==4){
                    System.out.println(pp[0]);

                }else if (p[1].length()==4){
                    System.out.println(pp[1]);
                }else {
                    System.out.println("ERROR");
                }
            }
        }
    }
}

发表于 2021-01-30 23:19:00 回复(0)
# 使用字典更加高效一点:使用字典来定义牌面值的大小
# 先判断是否有对王,有则输出对王
# 然后判断双方牌数是否相同,牌数相同时,按牌面值比,输出第一位较大的(包含**和**的比较)
# 当双方牌数不一样时,然后判断其中一方是否有**,有则输出**。否则,无法比较,输出ERROR。
while True:
    try:
        D = {'3':0, '4':1, '5':2, '6':3, '7':4, '8':5, '9':6, '10':7, 'J':8, 'Q':9, 'K':10, 'A':11, '2':12, 'joker':13, 'JOKER':14}
        a, b = input().split('-')
        s1, s2 = a.split(), b.split()
        if a == 'joker JOKER' or b == 'joker JOKER':
            print('joker JOKER')
        elif len(s1) == len(s2):
            print(a if D[s1[0]]>D[s2[0]] else b)
        elif len(s1) == 4:
            print(a)
        elif len(s2) == 4:
            print(b)
        else:
            print('ERROR')
    except:
        break

编辑于 2020-12-30 23:00:40 回复(0)
def bomb(s):
    s = s.split()
    if len(s) == 4 and s[0] == s[1] == s[2] == s[3]:
        return True
    else:
        return False
    
def tranform(s):
    s = s.replace('10', 'a')
    s = s.replace('J', 'b')
    s = s.replace('Q', 'c')
    s = s.replace('K', 'd')
    s = s.replace('A', 'e')
    s = s.replace('2', 'f')
    return s

while 1:
    try:
        s = input()
        S1, S2 = s.split('-')
        if 'joker JOKER' in s: 
            print('joker JOKER')
        else:
            if bomb(S1) and not bomb(S2):
                print(S1)
            elif not bomb(S1) and bomb(S2):
                print(S2)
            elif bomb(S1) and bomb(S2):
                print(S1 if tranform(S1) > tranform(S2) else S2)
            else:
                if len(S1.split()) != len(S2.split()):
                    print('ERROR')
                else:
                    print(S1 if tranform(S1) > tranform(S2) else S2)
    except:
        break

发表于 2020-09-22 16:25:47 回复(0)
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>

using namespace std;
//思路:
//通过"-"切割字符串,得到两个串
//通过" ",分别切割字符串,得到两个数组
//通过数组size 设定 flag 判断 输入的是 个子,对子,三子,**,顺子
//建立一个由3 -- JOKER,从小到达的数组,作为判断依据
//写入正常逻辑
int judge(vector<string> v)
{
    int flag = 0;
    if(v.size() == 1)    flag = 1;//个子
    else if(v.size() == 2)
    {
        if(v[0] == "joker" || v[1] == "JOKER")    flag = 6;//对王
        else    flag = 2;//普通顺子
    }
    else if(v.size() == 3)    flag = 3;//三个
    else if(v.size() == 4)    flag = 4;//**
    else if(v.size() == 5)    flag = 5;//顺子
    return flag;
}
//判断字符串是否合规
int myfind(vector<string> base,string v)
{
    for(int i = 0;i<base.size();i++)
    {
        if(v == base[i])    return i;
    }
     return -1;
}
int main()
{
    
    string str;
    while(getline(cin,str))
    {
        int index = str.find("-");
        string str1 = str.substr(0,index);
        string str2 = str.substr(index+1);
        
        vector<string> v1,v2;
        stringstream ss1(str1),ss2(str2);
        string tmp;
        while(ss1>>tmp)    v1.push_back(tmp);
        while(ss2>>tmp)   v2.push_back(tmp);

        int flag1 = judge(v1);
        int flag2 = judge(v2);
        vector<string> base={"3","4","5","6","7","8","9","10","J","Q","K","A","2","joker","JOKER"};
        //判断**的情况
        int win = 0;
        if(flag1==6||flag2==6 || flag1==4 || flag2==4)
        {
            if(flag1==6)    win =1;//🈶️一个对王
            else if (flag2==6)    win =2;
            else if(flag1 == flag2 && flag1 == 4)//两边都是**
            {
                auto it1 = myfind(base,v1[0]);//返回对应base的索引
                auto it2 = myfind(base,v2[0]);
                if(it1 != -1 && it2 != -1)//比较base的索引
                {
                    if(it1 > it2)    win = 1;
                    else win = 2;
                }else
                {
                    win = 0;
                }
            }
            else if(flag1 == 4 && flag2 != 4)    win = 1;//其中一边是**
            else if(flag1 != 4 && flag2 == 4)    win = 2;
        }
        else if(flag1 == flag2)
        {
            auto it1 = myfind(base,v1[0]);
            auto it2 = myfind(base,v2[0]);
            if(it1 != -1 && it2 != -1)
            {
                if(it1 > it2)    win = 1;
                else win = 2;
            }else
            {
                win = 0;
            }
        }else
        {
            win = 0;
        }
        
        if(win == 0)
        {
            cout<<"ERROR"<<endl;
        }
        else if(win == 1)
        {
            for(int i = 0;i<v1.size();i++)
            {
                cout<<v1[i]<<" ";
            }
            cout<<endl;
        }
        else if(win == 2)
        {
            for(int i = 0;i<v2.size();i++)
            {
                cout<<v2[i]<<" ";
            }
            cout<<endl;
        }
    }
    return 0;
}

发表于 2020-07-19 12:33:25 回复(0)
思路:判断“-”左右两边的字符串中空格的数量,对牌面进行类型标记,其中空格为1的时候,可能是对子,也可能是王炸,要分别讨论。然后取每一副牌的第一个字符,将其转化为int类型,作为牌面的大小。最后,根据牌面类型和牌面大小进行判断。
#include<iostream>
#include<string>

using namespace std;

int CalSpc(string str)
{
	int ans = 0;
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] == ' ')
			ans++;
	}
	return ans;
}

int CalChar(string str)
{
	int ans = 0;
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] != ' ')
			ans++;
	}
	return ans;
}

int Findnum(string str)
{
	int num;
	if (str == "10") num = 10;
	else if (str == "J") num = 11;
	else if (str == "Q") num = 12;
	else if (str == "K") num = 13;
	else if (str == "A") num = 14;
	else if (str == "2") num = 15;
	else if (str == "joker") num = 20;
	else if (str == "JOKER") num = 30;
	else num = str[0] - 48;
	return num;
}

class PokState
{
public:
	PokState(string s) :str(s)
	{

	}

	void CalPok()
	{
		int SpcNum = CalSpc(str);
		int CharNum = CalChar(str);

		if (SpcNum == 0)
		{
			PokType = 1;
			PokVal = Findnum(str);
		}
		else if (SpcNum == 1)
		{
			if (CharNum == 10)
			{
				PokType = 100;
				PokVal = 100;
			}
			else
			{
				PokType = 2;
				PokVal = Findnum(str.substr(0, str.find(' ')));
			}
		}
		else if (SpcNum == 2)
		{
			PokType = 3;
			PokVal = Findnum(str.substr(0, str.find(' ')));
		}
		else if (SpcNum == 3)
		{
			PokType = 10;
			PokVal = Findnum(str.substr(0, str.find(' ')));
		}
		else if (SpcNum == 4)
		{
			PokType = 5;
			PokVal = Findnum(str.substr(0, str.find(' ')));
		}
		else
		{
			PokType = -1;
			PokVal = -1;
		}
	}

	int PokType;
	int PokVal;
	string str;
};


int main()
{
	string s, LeftStr, RightStr;
	
	while (getline(cin, s))
	{
		LeftStr = s.substr(0, s.find('-'));
		RightStr = s.substr(s.find('-') + 1, s.size() - 1);
		
		PokState LS(LeftStr);
		PokState RS(RightStr);
		LS.CalPok();
		RS.CalPok();

		if (LS.PokType == RS.PokType)		//牌的类型相等,比较大小
		{
			if (LS.PokVal > RS.PokVal)
				cout << LS.str << endl;
			else
				cout << RS.str << endl;
		}
		else if (LS.PokType == 100 || (LS.PokType > RS.PokType && LS.PokType == 10))			//王炸 或者 炸比对面大,直接输出
			cout << LS.str << endl;
		else if (RS.PokType == 100 || (RS.PokType > LS.PokType && RS.PokType == 10))			//谁是王炸,直接输出
			cout << RS.str << endl;
		else
			cout << "ERROR" << endl;
	}

	return 0;
}


发表于 2020-07-05 12:26:48 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
/*
 * 输入:两个扑克牌序列
 * 输出:比较大的扑克牌序列
 * @author 码畜
 */
public class Main{
 public static BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
 public static String str;
 public static Map<String,Integer> map=new HashMap<String,Integer>();  //存储扑克牌的的大小优先级
 
 public static boolean allJoker(String[] temp){  //返回是否是双王
  if(temp[0].equals("joker")&&temp[1].equals("JOKER")||temp[0].equals("JOKER")&&temp[1].equals("joker")){
   return true;
  }
  return false;
 }
 
 public static boolean isSequece(String[] temp){  //返回是否是顺子
  if(temp[temp.length-1]=="2"||temp[temp.length-1]=="joker"||temp[temp.length-1]=="JOKER"){    //2,大小王不能出现在顺子中
   return false;
  }
  for(int i=1;i<temp.length;i++){
   if((map.get(temp[i])-map.get(temp[i-1]))!=1){
    return false;
   }
  }
  return true;
 }
 
 public static boolean allEqual(String[] temp){  //返回是否全等,处理对子,三牌,炸时使用
  String str=temp[0];
  for(String s:temp){
   if(!s.equals(str)){
    return false;
   }
  }
  return true;
 }
 
 public static String[] comparePart(String[] first,String[] last){  //非等额牌长情况处理
  if(first.length==2&&allJoker(first)){
   return first;
  }else if(last.length==2&&allJoker(last)){
   return last;
  }else if(first.length==4&&last.length!=4&&allEqual(first)){
   return first;
  }else if(last.length==4&&first.length!=4&&allEqual(last)){
   return last;
  }else{
   System.out.println("ERROR");      //其余情况:无法比较
   return null;
  }
  
 }
 
 public static String[] compare(String[] first,String[] last,int length){
  int[] firArr=new int[first.length];
  int[] lasArr=new int[last.length];
  //获取扑克牌优先级序列
  for(int i=0;i<first.length;i++){
   firArr[i]=map.get(first[i]);
  }
  for(int i=0;i<last.length;i++){
   lasArr[i]=map.get(last[i]);
  }
  /*
   * 单牌,对牌,三牌,炸,顺子都只需要比较第一张牌的大小,无需分类,只需验证是否属于这些类型
   * 但是两张牌和王炸的情况需要单独讨论
   */
  if(first.length==2&&allJoker(last)||last.length==2&&allJoker(first)){
   if(allJoker(last)){
    return last;
   }else{
    return first;
   }
  }
  
  if(allEqual(first)&&allEqual(last)||isSequece(first)&&isSequece(last)&&length>=5){
   if(firArr[0]>lasArr[0]){
    return first;
   }else{
    return last;
   }

  }else{
   System.out.println("ERROR");
   return null;
  }
 }
 
 public static void main(String[] args) throws IOException {
  map.put("3", 1);
  map.put("4", 2);
  map.put("5", 3);
  map.put("6", 4);
  map.put("7", 5);
  map.put("8", 6);
  map.put("9", 7);
  map.put("10",8);
  map.put("J", 9);
  map.put("Q", 10);
  map.put("K", 11);
  map.put("A", 12);
  map.put("2", 13);
  map.put("joker",14);
  map.put("JOKER",15);
  
  while(null!=(str=reader.readLine())){           //获取牌面信息
   String[] inputMsg=str.split("\\-");
   String[] firstCard=inputMsg[0].split(" ");
   String[] lastCard=inputMsg[1].split(" ");
   StringBuilder sb=new StringBuilder();
   if(firstCard.length!=lastCard.length){      //非等额牌长情况处理
    if(null!=comparePart(firstCard, lastCard)){
     for(String s:comparePart(firstCard, lastCard)){
      sb.append(s+" ");
     }
     sb.deleteCharAt(sb.length()-1);
     System.out.println(sb);
    }
   }else{
    //compare(firstCard,lastCard,firstCard.length);
    if(null!=compare(firstCard, lastCard,firstCard.length)){
     for(String s:compare(firstCard, lastCard,firstCard.length)){
      sb.append(s+" ");
     }
     sb.deleteCharAt(sb.length()-1);
     System.out.println(sb);
    }
   }
   
  }
 }
}

发表于 2020-06-24 21:26:04 回复(0)
直接看代码 简单明了
import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while((str = br.readLine()) != null){
            String[] poker = str.split("-");
            String[] card_1 = poker[0].split(" ");
            String[] card_2 = poker[1].split(" ");
            int a = check(card_1);
            int b = check(card_2);
            if(a == b){ //如果相等 则比较第一张牌
                if(change(card_1[0]) < change(card_2[0]))
                    System.out.println(poker[1]);
                else System.out.println(poker[0]);
            }else if(a == 6){
                System.out.println(poker[0]);
            }else if(b == 6){
                System.out.println(poker[1]);
            }else if(a == 4){
                System.out.println(poker[0]);
            }else if(b == 4){
                System.out.println(poker[1]);
            }else{
                System.out.println("ERROR");
            }
        }
    }
    //判断实际数字
    public static int change(String str){
        switch(str){
            case "J": return 11;
            case "Q": return 12;
            case "K": return 13;
            case "A": return 14;
            case "2": return 15;
            default: return Integer.parseInt(str);
        }
    }
    //判断牌类型
    public static int check(String[] card){
        int len = card.length;
        if(len == 1) return 1;
        else if(len == 2){
            if(card[0].equals("joker") || card[1].equals("joker")) return 6;
            return 2;
        }else if(len == 3) return 3;
        else if(len == 4) return 4;
        else return 5;
    }
}

发表于 2020-04-23 10:56:24 回复(1)
import java.util.*;

public class Main {

    public static void main(String[] args) {
/*
  通过字符串长度(特征)将输入映射到牌型,但是10的长度为2,故转换为X
*/
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String[] str = scanner.nextLine().split("-");
            Map<String, String> map = new HashMap<String, String>();
            Map<String, Integer> order = new HashMap<>();
            {
                    /*排序字典*/
                order.put("3", 1);
                order.put("4", 2);
                order.put("5", 3);
                order.put("6", 4);
                order.put("7", 5);
                order.put("8", 6);
                order.put("9", 7);
                order.put("X", 8);
                order.put("J", 9);
                order.put("Q", 10);
                order.put("K", 11);
                order.put("A", 12);
                order.put("2", 13);
            }
                        /*Mapping*/
            for (int i=0;i<2;i++) {
                str[i]=str[i].replaceAll("10","X");
                if (str[i].length() == 11) {
                    map.put(str[i], "王炸");
                }
                if (str[i].length() == 7) {
                    map.put(str[i], "炸弹");
                }
                if (str[i].length() == 9) {
                    map.put(str[i], "顺子");
                }
                if (str[i].length() == 1) {
                    map.put(str[i], "单牌");
                }
                if (str[i].length() == 3) {
                    map.put(str[i], "对子");
                }
                if (str[i].length() == 5) {
                    map.put(str[i], "三个");
                }

            }
            /*牌型相同*/
            if (map.get(str[0]) == map.get(str[1])) {
                if(map.get(str[0]) == "王炸"){
                    System.out.println(str[0]);
                } else{
            /*是否为单牌*/
                    if(str[0].length()>1){
                        String[] sample1 = str[0].split(" ");
                        String[] sample2 = str[1].split(" ");
                        System.out.println(order.get(sample1[0]) >=order.get(sample2[1])?str[0].replaceAll("X","10"):str[1].replaceAll("X","10"));
                    }else {
                        System.out.println(order.get(str[0])>=order.get(str[1])?str[0].replaceAll("X","10"):str[1].replaceAll("X","10"));
                    }

                }
            /*牌型不同*/
            } else if (map.get(str[0]) == "王炸" || map.get(str[1]) == "王炸") {
                System.out.println(map.get(str[0]) == "王炸" ? str[0] : str[1]);
            } else if (map.get(str[0]) == "炸弹" || map.get(str[1]) == "炸弹") {
                System.out.println(map.get(str[0]) == "炸弹" ? str[0] : str[1]);
            } else {
                System.out.println("ERROR");
            }

        }
    }
}


编辑于 2020-02-17 16:03:55 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String s = sc.nextLine();
            if(s.contains("joker JOKER")) {
                System.out.println("joker JOKER");
                continue;
            }
            String s1 = s.split("-")[0];
            s1 = s1.replaceAll("J", "11");
            s1 = s1.replaceAll("Q", "12");
            s1 = s1.replaceAll("K", "13");
            s1 = s1.replaceAll("A", "14");
            s1 = s1.replaceAll("2", "15");
            String[] s1s = s1.split(" ");
            String s2 = s.split("-")[1];
            s2 = s2.replaceAll("J", "11");
            s2 = s2.replaceAll("Q", "12");
            s2 = s2.replaceAll("K", "13");
            s2 = s2.replaceAll("A", "14");
            s1 = s1.replaceAll("2", "15");
            String[] s2s = s2.split(" ");
            if(s1s.length==4 && s2s.length!=4) {
                System.out.println(s.split("-")[0]);
            } else if(s1s.length!=4 && s2s.length==4) {
                System.out.println(s.split("-")[1]);
            } else if(s1s.length==4 && s2s.length==4) {
                if(Integer.valueOf(s1s[0]) > Integer.valueOf(s2s[0])) {
                    System.out.println(s.split("-")[0]);
                } else {
                    System.out.println(s.split("-")[1]);
                }
            } else if(s1s.length==s2s.length) {
                if(Integer.valueOf(s1s[0]) > Integer.valueOf(s2s[0])) {
                    System.out.println(s.split("-")[0]);
                } else {
                    System.out.println(s.split("-")[1]);
                }
            } else {
                System.out.println("ERROR");
            }
        }
    }
}

发表于 2018-10-09 13:13:39 回复(0)
#include <bits/stdc++.h>
using namespace std;

int DeleteSpace(string s)
{
    int cnt = 1;
    for (int i = 0; i<s.size(); i++)
    {
        if (s[i] == ' ') cnt++;
    }
    return cnt;
}

vector<string> table = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "joker", "JOKER" };

int Value(string s)
{
    int res;
    if (s[0] == '1') res = 10;
    else if (s[0] == '3') res = 3;
    else if (s[0] == '4') res = 4;
    else if (s[0] == '5') res = 5;
    else if (s[0] == '6') res = 6;
    else if (s[0] == '7') res = 7;
    else if (s[0] == '8') res = 8;
    else if (s[0] == '9') res = 9;
    else if (s[0] == 'J') res = 11;
    else if (s[0] == 'Q') res = 12;
    else if (s[0] == 'K') res = 13;
    else if (s[0] == 'A') res = 14;
    else if (s[0] == '2') res = 15;
    return res;
}

int main()
{
    string str;
    while (getline(cin, str))
    {
        string s1 = str.substr(0, str.find('-'));
        string s2 = str.substr(str.find('-') + 1);
        int n1 = DeleteSpace(s1);
        int n2 = DeleteSpace(s2);
        string Joker = "joker JOKER";
        string Poker = "345678910JQKA2";
        if (s1 == Joker) cout << s1 << endl;
        else if (s2 == Joker) cout << s2 << endl;
        else if (n1 == n2)
        {
            string f1, f2;
            f1 = s1.substr(0, s1.find(" "));
            f1 = s2.substr(0, s2.find(" "));
            int p1 = Value(s1);
            int p2 = Value(s2);
            //cout << p1 << endl;
            //cout << p2 << endl;
            //auto num1 = find(table.begin(), table.end(), f1);
            //auto num2 = find(table.begin(), table.end(), f2);
            if (p1>p2) cout << s1 << endl;
            else cout << s2 << endl;
        }
        else if (n1 == 4) cout << s1 << endl;
        else if (n2 == 4) cout << s2 << endl;
        else cout << "ERROR" << endl;
    }
    system("pause");
    return 0;    
} 

发表于 2018-08-06 22:13:53 回复(0)
import java.util.*;
public class Main{
    static String cell(String str1,String str2){//str1=J J J J    str2=3 3 3 3
        String[] str3=str1.split(" ");//JJJJ
        String[] str4=str2.split(" ");//3333
        String[] array={"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "joker", "JOKER"};
        if(str1.equals("joker JOKER")||str2.equals("joker JOKER")){   //先考虑最大的牌
                return "joker JOKER";
        }else if(str3.length==4||str4.length==4){   //为**
                    if(str3.length==4&&str4.length==4){
                        for(int i=0;i<array.length;i++){
                            if(str3[0].equals(array[i])){
                                 return str2;
                            }
                            if(str4[0].equals(array[i])){
                                return str1;
                            }
                        }
                 }else{
                     if(str3.length!=4){
                         return str2;
                     }else{
                         return str1;
                     }
                 }    
        }else if(str3.length==str4.length){   //比较同种类型的牌
            for(int i=0;i<array.length;i++){
                if(str3[0].equals(array[i])){
                    return str2;
                }
                if(str4[0].equals(array[i])){
                    return str1;
                }
            }
        }
        return "ERROR";
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
        String[] str=sc.nextLine().split("-");
        String str1=str[0];
        String str2=str[1];
        System.out.println(cell(str1,str2));
        }    
    }
}
发表于 2018-07-27 21:24:19 回复(0)