题目 1975: 蓝桥杯算法提高VIP-扑克排序

package LanQiao.C_Web.C_1975;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

/**
 * @author Huiex on 2022/3/12
 */
public class C1975 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String str = input.next();

        ArrayList<Poker> list = new ArrayList();

        int i = 0;
        while(i < str.length()){
            if (str.charAt(i) <= '9' && str.charAt(i) >= 2){
                list.add(new Poker("" + str.charAt(i),str.charAt(i+1)));
                i = i + 2;
            }else if (str.charAt(i) == 'J') {
                list.add(new Poker("J",str.charAt(i+1)));
                i = i + 2;
            }else if (str.charAt(i) == 'Q') {
                list.add(new Poker("Q",str.charAt(i+1)));
                i = i + 2;
            }else if (str.charAt(i) == 'K') {
                list.add(new Poker("K",str.charAt(i+1)));
                i = i + 2;
            }else if (str.charAt(i) == 'A') {
                list.add(new Poker("A",str.charAt(i+1)));
                i = i + 2;
            }else{
                list.add(new Poker("10", str.charAt(i+2)));
                i = i + 3;
            }
        }

        //进行比较排序
        list.sort(new Comparator<Poker>(){

            public int compare(Poker o1,Poker o2){
                if(o2.num > o1.num) return -1;
                else if (o2.num < o1.num) return 1;
                else {
                    if(o2.color > o1.color) return -1;
                    else if (o2.color < o1.color) return 1;
                    else return 0;
                }
            }
        });

        //输出
        for(Poker result : list){
            System.out.print(result.num_o + result.color_o + " ");
        }
    }
}


class Poker{

    //原本的数值 和 花色
    String num_o;
    char color_o;


    //用于比较的 数值 和 花色
    int num;
    char color;

    public Poker(String num,char color){

        //将用于比较的数值和花色赋值
        if (num == "J") this.num = 11;
        else if(num == "Q") this.num = 12;
        else if (num == "K") this.num = 13;
        else if (num == "A") this.num = 14;
        else this.num = Integer.parseInt(num);

        if(color == 'd') this.color = 1;
        else if (color == 'c') this.color = 2;
        else if (color == 'h') this.color = 3;
        else if (color == 's') this.color = 4;

        //将原本的赋值
        this.num_o = num;
        this.color_o = color;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值