백준 Buffoon

KIMYEONGJUN·2025년 1월 25일
0
post-thumbnail

문제

내가 생각했을때 문제에서 원하는부분

The first line of input contains an integer N, (2 ≤ N ≤ 104). The next N lines will contain N positive integers v1, . . . , vN , one on each line, corresponding to the number of votes each candidate received, in order of registration.
Since the Kingdom of matchings population is 100,000 people, the total number of votes will not exceed this value, i.e Σvi ≤ 100,000.

Your program must output a single line containing the character ‘S’ if young Carlos is elected as buffoon, or the character ‘N’ otherwise.

내가 이 문제를 보고 생각해본 부분

입력 처리:
BufferedReader를 사용해 입력(System.in)에서 데이터를 읽는다.
첫 번째 줄에서 후보자 수 candidateCount를 읽어 정수로 변환한다.
배열 선언: 각 후보자의 투표 수를 저장하기 위한 배열 votes를 선언한다.
투표 수 입력: 반복문을 통해 각 후보자의 투표 수를 입력받아 votes 배열에 저장한다.
Carlos의 투표 수와 초기 최대 투표 수 설정:
carlosVotes는 첫 번째 후보자인 Carlos의 투표 수를 저장한다.
highestVotes도 초기값으로 Carlos의 투표 수를 설정한다.
최대 투표 수 탐색:
첫 번째 후보자를 제외한 나머지 후보자들의 투표 수를 비교하여 highestVotes를 갱신한다.
이 반복문을 통해 가장 높은 투표 수를 가진 후보자의 투표 수를 찾는다.
Carlos의 선출 여부 판단:
highestVotes가 carlosVotes와 같은지 비교하여 Carlos가 가장 많은 투표 수를 가졌는지 확인한다.
결과에 따라 'S' 또는 'N'을 출력한다.

코드로 구현

package baekjoon.baekjoon_26;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

// 백준 17530번 문제
public class Main912 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int candidateCount = Integer.parseInt(br.readLine());
        int[] votes = new int[candidateCount];

        // 후보자들의 투표 수를 배열에 저장
        for(int i = 0; i < candidateCount; i++) {
            votes[i] = Integer.parseInt(br.readLine());
        }

        int carlosVotes = votes[0]; // Carlos의 투표 수
        int highestVotes = carlosVotes; // 초기값을 Carlos의 투표 수로 설정

        // 최대 투표 수를 찾기
        for(int i = 1; i < candidateCount; i++) {
            if(votes[i] > highestVotes) {
                highestVotes = votes[i];
            }
        }

        // Carlos가 가장 높은 투표 수를 가졌는지 확인
        boolean isCarlosElected = (highestVotes == carlosVotes);
        System.out.println(isCarlosElected ? 'S' : 'N');
        br.close();
    }
}

마무리

코드와 설명이 부족할수 있습니다. 코드를 보시고 문제가 있거나 코드 개선이 필요한 부분이 있다면 댓글로 말해주시면 감사한 마음으로 참고해 코드를 수정 하겠습니다.

profile
Junior backend developer

0개의 댓글

관련 채용 정보