华为OD机试-整理扑克牌-2022Q4 A卷-Py/Java/JS

给定一组数字,表示扑克牌的牌面数字,忽略扑克牌的花色,请按如下规则对这一组扑克牌进行整理:

步骤1、对扑克牌进行分组,形成组合牌,规则如下:当牌面数字相同张数大于等于4时,组合牌为“炸弹”:3张相同牌面数字 + 2张相同牌面数字,且3张牌与2张牌不相同时,组合牌为“葫芦”
3张相同牌面数字,组合牌为“三张”
2张相同牌面数字,组合牌为“对子”
剩余没有相同的牌,则为“单张”:
步骤2、对上述组合牌进行由大到小排列,规则如下:不同类型组合牌之间由大到小排列规则:“炸弹” >"葫芦”>"三张”>"对子”> “单张”:相同类型组合牌之间,除“葫芦”外,按组合牌全部牌面数字加总由大到小排列:"葫芦”则先按3张相同牌面数字加总由大到小排列,3张相同牌面数字加总相同时,再按另外2张牌面数字加总由大到小排列;
由于“葫芦”>“三张”,因此如果能形成更大的组合牌,也可以将“三张”拆分为2张和1张,其中的2张可以和其它“三张”重新组合成“葫芦”,剩下的1张为“单张”

步骤3、当存在多个可能组合方案时,按如下规则排序取最大的一个组合方案:依次对组合方案中的组合牌进行大小比较,规则同上:当组合方案A中的第n个组合牌大于组合方案B中的第n个组合牌时,组合方案A大于组合方案B;
输入描述:
第一行为空格分隔的N个正整数,每个整数取值范围[1,13],N的取值范围[1,1000]

输出描述:
经重新排列后的扑克牌数字列表,每个数字以空格分隔

示例1
输入:
1 3 3 3 2 1 5
输出:
3 3 3 1 1 5 2
示例2
输入:
4 4 2 1 2 1 3 3 3 4
输出:
4 4 4 3 3 2 2 1 1 3

Java 代码

import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
import java.math.BigInteger;
 
class Main {
	public static void main(String[] args) {
        // 处理输入
        Scanner in = new Scanner(System.in);
        String[] card_strs = in.nextLine().split(" ");
 
        int[] cards = new int[14];
        for (String card : card_strs) {
            cards[Integer.parseInt(card)]++;
        }
        
        ArrayList boomb = new ArrayList<>();
        int i = 13;
        while (i >= 1) {
            if (cards[i] >= 4) {
                cards[i] -= 4;
                String num = Integer.toString(i);
                boomb.add(num + num + num + num);
                continue;
            }
            i--;
        }
 
        boomb.sort(new Comparator() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.parseInt(o2) - Integer.parseInt(o1);
            }
        });
 
        ArrayList three_cards = new ArrayList<>();
        i = 13;
        while (i >= 1) {
            if (cards[i] >= 3) {
                cards[i] -= 3;
                String num = Integer.toString(i);
                three_cards.add(num + num + num);
                continue;
            }
            i--;
        }
 
        ArrayList two_cards = new ArrayList<>();
        i = 13;
        while (i >= 1) {
            if (cards[i] >= 2) {
                cards[i] -= 2;
                String num = Integer.toString(i);
                two_cards.add(num + num);
                continue;
            }
            i--;
        }
 
        ArrayList single_cards = new ArrayList<>();
        i = 13;
        while (i >= 1) {
            if (cards[i] >= 1) {
                cards[i] -= 1;
                String num = Integer.toString(i);
                single_cards.add(num);
                continue;
            }
            i--;
        }
 
        ArrayList hulu = new ArrayList<>();
        int size = Math.min(three_cards.size(), two_cards.size());
        for (int j = 0; j < size; j++) {
            hulu.add(three_cards.get(0) + two_cards.get(0));
            three_cards.remove(0);
            two_cards.remove(0);
        }
 
        // 输出
        ArrayList result = new ArrayList<>();
        result.addAll(boomb);
        result.addAll(hulu);
        result.addAll(three_cards);
        result.addAll(two_cards);
        result.addAll(single_cards);
 
        for (String item : result) {
            char[] chars = item.toCharArray();
            for (char c : chars){
                System.out.print(c + " ");
            }
        }
    }
 
}

Python代码

import functools
import sys
from collections import Counter, defaultdict
import copy
from itertools import permutations
import re
import math
import sys
 
def cmp1(a, b):
    return b[0] * b[1] - a[0] * a[1]
 
 
def cmp2(a, b):
    return int(b)-int(a)
 
#处理输入
nums = [int(x) for x in input().split(" ")]
 
cards =[0 for i in range(14)]
for num in nums:
    cards[num]+=1
 
boomb = []
i = 13
while (i >= 1):
    if (cards[i] >= 4):
        cards[i] -= 4
        temp_str = str(i)
        boomb.append(temp_str + temp_str + temp_str + temp_str)
        continue
    i-=1
 
boomb.sort(key=functools.cmp_to_key(cmp2))
 
three_cards = []
i = 13
while (i >= 1):
    if (cards[i] >= 3):
        cards[i] -= 3
        temp_str = str(i)
        three_cards.append(temp_str + temp_str + temp_str)
        continue
    i-=1
 
 
two_cards = []
i = 13
while (i >= 1):
    if (cards[i] >= 2):
        cards[i] -= 2
        temp_str = str(i)
        two_cards.append(temp_str + temp_str)
        continue
    i-=1
 
 
single_cards = []
i = 13
while (i >= 1):
    if (cards[i] >= 1):
        cards[i] -= 1
        temp_str = str(i)
        single_cards.append(temp_str)
        continue
    i-=1
 
hulu = []
size = min(len(three_cards), len(two_cards))
for j in range(size):
    hulu.append(three_cards[0] + two_cards[0])
    three_cards.pop(0)
    two_cards.pop(0)
 
 
result = []
 
for item in boomb + hulu + three_cards + two_cards + single_cards:
    for single_char in item:
        result.append(single_char)
print(" ".join(result))

JS代码

function output(output_strs) {
    let str = "";
    for (let i=0;i= 1) {
        if (cards[i] >= 4) {
            cards[i] -= 4;
            let num = i.toString()
            boomb.push(num + num + num + num);
            continue;
        }
        i--;
    }
 
    boomb.sort(function(a, b) {
        return parseInt(b) - parseInt(a)
    });
 
    let three_cards = new Array();
    i = 13;
    while (i >= 1) {
        if (cards[i] >= 3) {
            cards[i] -= 3;
            let num = i.toString()
            three_cards.push(num + num + num);
            continue;
        }
        i--;
    }
 
    let two_cards = new Array();
    i = 13;
    while (i >= 1) {
        if (cards[i] >= 2) {
            cards[i] -= 2;
            let num = i.toString()
            two_cards.push(num + num);
            continue;
        }
        i--;
    }
 
    let single_cards = new Array();
    i = 13;
    while (i >= 1) {
        if (cards[i] >= 1) {
            cards[i] -= 1;
            let num = i.toString()
            single_cards.push(num);
            continue;
        }
        i--;
    }
 
    let hulu = new Array();
    let size = Math.min(three_cards.length, two_cards.length);
    for (let j = 0; j < size; j++) {
        hulu.push(three_cards[0] + two_cards[0]);
        three_cards.shift();
        two_cards.shift();
    }
 
    // 输出
    let res = ""
    res += output(boomb);
    res += output(hulu);
    res += output(three_cards);
    res += output(two_cards);
    res += output(single_cards);
    console.log(res)
}
 
main("4 4 2 1 2 1 3 3 3 4")

你可能感兴趣的:(java,javascript,华为,python)