happy coding

[java] 27736. 찬반 투표 본문

coding study/baekjoon

[java] 27736. 찬반 투표

yeoonii 2023. 7. 25. 01:32
import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
    public static void main(String[] args) throws IOException {
        //중앙대학교 재학생의 수 n 입력받음
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        //n개의 투표 내역이 공백으로 구분되어 주어짐. 찬성 1 반대 -1 기권 0
        StringTokenizer st = new StringTokenizer(br.readLine());
        int[] arr = new int[n];
        
        int app = 0;    //찬성
        int opp = 0;    //반대
        int abst = 0;   //기권
        
        for (int i = 0; i < arr.length; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
            if (arr[i] == 1) {
                app++;
            } else if (arr[i] == -1) {
                opp++;
            } else if (arr[i] == 0) {
                abst++;
            }
        }
        //기권자 수가 재학생 절반 이상이면 무효 처리
        if (abst >= n/2) {
            System.out.println("INVALID");
        }
        //출력
        if (app > opp) {
            System.out.println("APPROVED");
        } else {
            System.out.println("REJECTED");
        }
    }
}

잘 짰다고 생각했는데 실패했다 ^>^ 왜지

아 절반 이상인 경우를 안나눠서 그런 것 + else if 로 묶는 것 을 안했다

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

public class Main {
    public static void main(String[] args) throws IOException {
        //중앙대학교 재학생의 수 n 입력받음
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        //n개의 투표 내역이 공백으로 구분되어 주어짐
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        int app = 0;    //찬성
        int opp = 0;    //반대
        int abst = 0;   //기권
        
        //찬성 1 반대 -1 기권 0
        for (int i = 0; i < n; i++) {
            int output = Integer.parseInt(st.nextToken());
            if (output == 1) {
                app++;
            } else if (output == -1) {
                opp++;
            } else if (output == 0) {
                abst++;
            }
        }
        //절반 이상 선언
        int half = 0;
        if (n%2 != 0) {
            half = (n/2)+1;
        } else {
            half = n/2;
        }
        //기권자 수가 재학생 절반 이상이면 무효 처리
        if (abst >= half) {
            System.out.println("INVALID");
        } else if (app > opp) {
            System.out.println("APPROVED");
        } else {
            System.out.println("REJECTED");
        }
    }
}

'coding study > baekjoon' 카테고리의 다른 글

[java] 10818. 최대, 최소  (0) 2023.07.25
[java] 27465. 소수가 아닌 수  (0) 2023.07.25
[java] 27960. 사격 내기  (0) 2023.07.25
[java] 28014. 첨탑 밀어서 부수기  (0) 2023.07.24
[java] 28061. 레몬 따기  (0) 2023.07.24
Comments