Koala - 10기/기초 알고리즘 스터디

[백준/JAVA] 2511번 카드놀이

2_donghh 2023. 3. 19. 22:58

https://www.acmicpc.net/problem/2511

 

2511번: 카드놀이

첫 번째 줄에는 게임이 끝난 후, A와 B가 받은 총 승점을 순서대로 빈칸을 사이에 두고 출력한다. 두 번째 줄에는 이긴 사람이 A인지 B인지 결정해서, 이긴 사람을 문자 A 또는 B로 출력한다. 만약

www.acmicpc.net

[입력]

입력 파일은 두 개의 줄로 이루어진다. 첫 번째 줄에는 A가 늘어놓은 카드의 숫자들이 빈칸(공백)을 사이에 두고 순서대로 주어진다. 두 번째 줄에는 B가 늘어놓은 카드의 숫자들이 빈칸(공백)을 사이에 두고 순서대로 주어진다.

[출력]

첫 번째 줄에는 게임이 끝난 후, A와 B가 받은 총 승점을 순서대로 빈칸을 사이에 두고 출력한다. 두 번째 줄에는 이긴 사람이 A인지 B인지 결정해서, 이긴 사람을 문자 A 또는 B로 출력한다. 만약 비기는 경우에는 문자 D를 출력한다.

[코드]

[JAVA]

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int[] a = new int[10], b = new int[10];
        int aScore = 0, bScore = 0;
        for (int i = 0 ; i < 10 ; i++){
            a[i] = input.nextInt();
        }
        for (int i = 0 ; i < 10 ; i++){
            b[i] = input.nextInt();
        }
        for (int i = 0 ; i < 10; i++){
            if (a[i] > b[i]){
                aScore += 3;
            }
            else if (b[i] > a[i]){
                bScore += 3;
            }
            else {
                aScore += 1;
                bScore += 1;
            }
        }

        if(aScore > bScore) System.out.println(aScore +" "+ bScore +"\\nA");

        else if(aScore < bScore) System.out.println(aScore +" "+ bScore +"\\nB");

        else{         // aScore == bScore 일때 마지막으로 이긴사람을 확인해서 처리해줘야 함
            for(int i = 0 ; i < 10 ; i++){
                if(a[9-i] > b[9-i]){
                    System.out.println(aScore +" "+ bScore +"\\nA");
                    break;
                }

                else if(a[9-i] < b[9-i]){
                    System.out.println(aScore +" "+ bScore +"\\nB");
                    break;
                }

                else{
                    if (i != 9){
                        continue;
                    }
                    else {
                        System.out.println(aScore +" "+ bScore +"\\nD");
                    }
                }
            }
        }

    }

}

[배운점]

위의 코드에서 총합이 같을 경우 반복문을 통해서 또 다시 비교하여 무승부인지 A가 승리했는지 B가 승리했는지 확인하였는데 위의 반복문에서 A 배열과 B 배열의 인덱스를 비교할 때 이를 한 번에 처리 할 수 있음을 알게 되었다.

→ 더 나은 코드

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int[] arrA = new int[10];
		int[] arrB = new int[10];
		int scoreA = 0;
		int scoreB = 0;
		int dCount = 0; // 각각의 배열 인덱스를 비교할 때 비교횟수를 카운트
		int lastIdx = 0; // A 또는 B가 승리한 마지막 인덱스 값
		
		for(int i=0;i<10;i++) {
			arrA[i] = input.nextInt();
		}

		for(int i=0;i<10;i++) {
			arrB[i] = input.nextInt();
		}

		for(int i=0;i<10;i++) {
			if (arrA[i] > arrB[i]) {
				scoreA+=3;
				lastIdx = i;
			}
			else if (arrA[i] < arrB[i]) {
				scoreB+=3;
				lastIdx = i;
			}
			else {
				scoreA++;
				scoreB++;
				dCount++;
			}
		}
		System.out.printf("%d %d\\n", scoreA, scoreB);

		if (scoreA > scoreB) {
			System.out.println('A');
		}

		else if (scoreB > scoreA) {
			System.out.println('B');
		}

		else { // 총합이 같은경우
			if (dCount==10) { // 모두 비긴 경우
				System.out.println('D');
			}

			else {    // 마지막으로 승부가 결정된 인덱스를 이용하여 A와 B를 비교해준다
				if(arrA[lastIdx] > arrB[lastIdx]) {
					System.out.println('A');
				}
				else {
					System.out.println('B');
				}
			}
		}
	}

}