Algorithm/BOJ(Baekjoon Online Judge)

[백준 - 7360번] Undercut - Java //Wello Horld //

koucop 2020. 4. 16. 10:43

 

이번에는 BOJ의 7360번 문제 "Undercut" 을 풀어보도록 하자

 

 

 

 

성공한 코드는 다음과 같다.

 

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        String line = br.readLine();
        while (Integer.parseInt(line) != 0) {
            int r = Integer.parseInt(line);
            int scoreA = 0;
            int scoreB = 0;
            int[] A = new int[r];
            int[] B = new int[r];
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int i = 0; i < r; i++) {
                A[i] = Integer.parseInt(st.nextToken());
            }
            st = new StringTokenizer(br.readLine());
            for (int i = 0; i < r; i++) {
                B[i] = Integer.parseInt(st.nextToken());
            }
            for (int i = 0; i < r; i++) {
                if (A[i] != B[i]) {
                    if (A[i] - 1 == B[i]) {
                        if (A[i] == 2) {
                            scoreB += 6;
                        } else {
                            scoreB += A[i] + B[i];
                        }
                    } else if (B[i] - 1 == A[i]) {
                        if (B[i] == 2) {
                            scoreA += 6;
                        } else {
                            scoreA += A[i] + B[i];
                        }
                    } else if (A[i] > B[i]) {
                        scoreA += A[i];
                    } else if (B[i] > A[i]) {
                        scoreB += B[i];
                    }
                }
            }
            bw.write("A has " + scoreA + " points. B has " + scoreB + " points.\n");
            line = br.readLine();
            if (Integer.parseInt(line) == 0) {
                break;
            } else {
                bw.write("\n");
            }
        }

        bw.flush();
        br.close();
        bw.close();
    }
}

 

 

문제 : https://www.acmicpc.net/problem/7360

 

7360번: Undercut

Undercut is a card game where two players each have five cards numbered one through five. At each round, each player selects a card, then simultaneously reveals it. If the cards are of equal value, there is no score. Otherwise, there are two cases: the two

www.acmicpc.net

 

 

혹시 코드에 이상한 부분이나 틀린 부분이 있던지, 이해가 안가는 부분이 있다면 댓글로 알려주세요