프로그래밍/코드업 일기

코드업(CodeUp) 4721 : 투표

Jaebins 2023. 8. 11. 13:37
반응형
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	static int n;
	static int[][] votes;
	static int[] votesSum;
	static int rs_index;
	static int loopCnt;
	
	public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        votes = new int[n][3];
        votesSum = new int[3];

       for(int i = 0; i < votes.length; i++) {
           for(int j = 0; j < 3; j++) {
               votes[i][j] = sc.nextInt();
               votesSum[j] += votes[i][j];
           }
       }

       int[] sameVotesSum = makeSameVotesSumArr(votesSum);
       int[] cnts = new int[3];

       for(int i = 3; i > 0; i--) {
           for(int j = 0; j < n; j++) {
               for(int k = 0; k < 3; k++) {
                   if(sameVotesSum[k] == 0) continue;
                   if(votes[j][k] == i) cnts[k] += i;
               }
           }

           int checkNumberCnt = 0;
           int[] sameVotesSum_empty = makeSameVotesSumArr(cnts);

           for(int j = 0; j < 3; j++) {
               if(sameVotesSum_empty[j] != 0) checkNumberCnt++;
           }

           if(checkNumberCnt >= 2) {
               loopCnt++;
               continue;
           }

           break;
       }
       System.out.printf("%d %d\n", loopCnt < 3 ? rs_index + 1 : 0, sameVotesSum[rs_index]);
    }

    static int[] makeSameVotesSumArr(int[] sumArr) {
        int[] maxSumArr = Arrays.copyOf(sumArr, sumArr.length);
        Arrays.sort(maxSumArr);
        int maxSum = maxSumArr[2];

        int[] rs = new int[3];

        for(int i = 0; i < 3; i++) {
            if(maxSum == sumArr[i]) {
                rs[i] = votesSum[i];
                rs_index = i;
            }
        }

        return rs;
    }
}
반응형