Java实现蓝桥杯算法提高12-2扑克排序

扑克牌排序

问题描述
  扑克牌排序:构造扑克牌数组,对扑克牌进行排序。
排序原则如下:数字从小到大是2-10、J、Q、K和A,花色从小到大是方块(diamond)、梅花(club)、红桃(heart)、黑桃(spade)。
两张牌比较时先看数字,数字相同时看花色。要求对输入的扑克牌进行从小到大排序。

输入五张牌(表示黑桃2、红桃3、黑桃3、方块A和梅花J): 2s3h3sAdJc
  输出结果应为:2 s 3 h 3 s J c A d数组长度固定为5。

package 第六次模拟;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Demo11扑克牌 {

 
	    public static void main(String[] args) {
	        Scanner sc = new Scanner(System.in);
	        String s = sc.next();
	        String[] s1 = s.split("d|c|h|s");
	        A[] a = new A[s1.length];
	        for (int i = 0; i < s1.length; i++) {
	            a[i] = new A();
	            a[i].data1 = s1[i];
	            if (s1[i].equals("10")) {
	                a[i].data = "91";
	            }
	            if (s1[i].equals("J")) {
	                a[i].data = "92";
	                continue;
	            } else if (s1[i].equals("Q")) {
	                a[i].data = "93";
	                continue;
	            } else if (s1[i].equals("K")) {
	                a[i].data = "94";
	                continue;
	            } else if (s1[i].equals("A")) {
	                a[i].data = "95";
	                continue;
	            }
	            a[i].data = s1[i];
	        }
	        List<A> list = new ArrayList<A>();
	        int h = 0;
	        for (int i = 0; i < s.length(); i++) {
	            if (s.charAt(i) == 'd') {
	                a[h].color = 1;
	                a[h].color1 = "d";
	                list.add(a[h++]);
	            } else if (s.charAt(i) == 'c') {
	                a[h].color = 2;
	                a[h].color1 = "c";
	                list.add(a[h++]);
	            } else if (s.charAt(i) == 'h') {
	                a[h].color = 3;
	                a[h].color1 = "h";
	                list.add(a[h++]);
	            } else if (s.charAt(i) == 's') {
	                a[h].color = 4;
	                a[h].color1 = "s";
	                list.add(a[h++]);
	            }

	        }
	        Collections.sort(list);
	        for (A b : list) {
	            System.out.print(b.data1 + b.color1 + " ");
	        }
	    }

	

static	class A implements Comparable<A> {
	    String data;
	    String data1;
	    String color1;
	    int color;

	    @Override
	    public int compareTo(A o) {
	        if (data.equals(o.data)) {
	            return color - o.color;
	        } else {
	            return data.compareTo(o.data);
	        }
	    }
	
}
}
原文地址:https://www.cnblogs.com/a1439775520/p/13075994.html