java 입문--4111: 게임 승자 판단-Who Is the Winner

3832 단어
기초 문제에서 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");
        }

    }


}

좋은 웹페이지 즐겨찾기