华为OD 斗地主之顺子

题目描述

在斗地主扑克牌游戏中, 扑克牌由小到大的顺序为:3,4,5,6,7,8,9,10,J,Q,K,A,2,玩家可以出的扑克牌阵型有:单张、对子、顺子、飞机、炸弹等。

其中顺子的出牌规则为:由至少5张由小到大连续递增的扑克牌组成,且不能包含2。

例如:{3,4,5,6,7}、{3,4,5,6,7,8,9,10,J,Q,K,A}都是有效的顺子;而{J,Q,K,A,2}、 {2,3,4,5,6}、{3,4,5,6}、{3,4,5,6,8}等都不是顺子。

给定一个包含13张牌的数组,如果有满足出牌规则的顺子,请输出顺子。

如果存在多个顺子,请每行输出一个顺子,且需要按顺子的第一张牌的大小(必须从小到大)依次输出。

如果没有满足出牌规则的顺子,请输出No。

输入描述

13张任意顺序的扑克牌,每张扑克牌数字用空格隔开,每张扑克牌的数字都是合法的,并且不包括大小王:2 9 J 2 3 4 K A 7 9 A 5 6

不需要考虑输入为异常字符的情况

输出描述

组成的顺子,每张扑克牌数字用空格隔开:3 4 5 6 7

用例1

输入

2 9 J 2 3 4 K A 7 9 A 5 6

输出

3 4 5 6 7

用例2

输入

2 9 J 10 3 4 K A 7 Q A 5 6

输出

3 4 5 6 7
9 10 J Q K A
import java.util.Scanner;

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner ins = new Scanner(System.in);
        String[] intput = ins.nextLine().split(" ");
        Map<String, Integer> map = new HashMap<>();
        List<String> outputList = new ArrayList<>();
        List<String> firstMatch = new ArrayList<>();
        Arrays.asList(intput).forEach(e->{
            map.put(e, 1);     
        });
        int count = 0;
        for(int i=3; i< 15; i++){
          if(map.get(match(i)) != null){
              outputList.add(match(i));
              count ++;
          }else{
              if(count < 5){
                  outputList.clear();
                  count = 0;
              }else{
                  firstMatch = new ArrayList<>(outputList);
                  outputList.clear();
                  count = 0;
              }
          }    
        }
        if(firstMatch.size()!=0){
            System.out.println(output(firstMatch));
        }
        if(count >= 5){
            System.out.println(output(outputList));
        }
        if(firstMatch.isEmpty() && count < 5){
            System.out.println("No");
        }
    }

    public static String match(int a){
        switch (a){
            case 11: return "J";
            case 12: return "Q";
            case 13:return "K";
            case 14:return "A";
            default: return String.valueOf(a);
        }
    }

    public static String output(List<String> list){
        StringBuffer buffer = new StringBuffer();
        list.forEach(e->{
            buffer.append(e).append(" ");
        });
        return buffer.toString().substring(0, buffer.length() - 1);
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值