zoukankan      html  css  js  c++  java
  • java入门--4111:判断游戏胜者-Who Is the Winner

    基础的题目 学习了StringBuilder, 通过delete来清空它 学了Map的简单用法

    import java.util.*;
    
    public class Main {
    
    
        public static int onepart_count(String s) {
            boolean newpart = false;
            int cnt = 0;
    
            for (int i = 0; i< s.length(); ++i) {
                if (!newpart && s.charAt(i) == '1') {
                    cnt += 1;
                    newpart = true;
                } else if (newpart && s.charAt(i) == '0')
                    newpart = false;
            }
            return cnt;
        }
    
        public static void main(String[] args) {
            Map<Character, String> m = new HashMap<Character, String>();
            m.put('0', "0000");
            m.put('1', "0001");
            m.put('2', "0010");
            m.put('3', "0011");
            m.put('4', "0100");
            m.put('5', "0101");
            m.put('6', "0110");
            m.put('7', "0111");
            m.put('8', "1000");
            m.put('9', "1001");
            m.put('a', "1010");
            m.put('b', "1011");
            m.put('c', "1100");
            m.put('d', "1101");
            m.put('e', "1110");
            m.put('f', "1111");
    
    
            Scanner scan = new Scanner(System.in);
            int rowcount = scan.nextInt();
            for (int i = 0; i < rowcount; ++i) {
                String alice = scan.next();
                String bob = scan.next();
                StringBuilder sb = new StringBuilder();
                for (int j = 2; j < alice.length(); ++j) {
                    Character c = alice.charAt(j);
                    sb.append(m.get(c));
                }
                int cnt_alice = onepart_count(sb.toString());
                sb.delete(0, sb.length());
                for (int j = 2; j < bob.length(); ++j) {
                    Character c = bob.charAt(j);
                    sb.append(m.get(c));
                }
                int cnt_bob = onepart_count(sb.toString());
                if (cnt_alice > cnt_bob) {
                    System.out.println("Alice");
                } else if (cnt_alice == cnt_bob) {
                    System.out.println("Tie");
                }
                else
                    System.out.println("Bob");
            }
    
        }
    
    
    }
  • 相关阅读:
    VS 2010 制作 Windows Service 安装包
    Postback 之后保持浏览器滚动条的位置
    Stream之list转map及问题解决
    List.sort()排序功能
    The content of element type "resultMap"
    MyBatis 一对一,一对多,多对多
    Java Array、List、Set互相转化
    Java流(Stream)操作实例-筛选、映射、查找匹配
    JAVA系列笔记十八之nohup实现后台运行程序
    VSCode汇总
  • 原文地址:https://www.cnblogs.com/kwliu/p/4727513.html
Copyright © 2011-2022 走看看