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");
        }

    }


}

 

posted on 2015-08-13 15:45  一颗麦粒  阅读(373)  评论(0编辑  收藏  举报

导航