알고리즘 문제풀이/Beakjoon

백준 5753. Pascal Library (feat. Chat GPT)

joah.k 2023. 2. 11. 18:36
728x90

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

 

5753번: Pascal Library

Pascal University, one of the oldest in the country, needs to renovate its Library Building, because after all these centuries the building started to show the effects of supporting the weight of the enormous amount of books it houses. To help in the renov

www.acmicpc.net

Pascal Library 

 
시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB 172 117 100 65.359%

문제

Pascal University, one of the oldest in the country, needs to renovate its Library Building, because after all these centuries the building started to show the effects of supporting the weight of the enormous amount of books it houses.

To help in the renovation, the Alumni Association of the University decided to organize a series of fund-raising dinners, for which all alumni were invited. These events proved to be a huge success and several were organized during the past year. (One of the reasons for the success of this initiative seems to be the fact that students that went through the Pascal system of education have fond memories of that time and would love to see a renovated Pascal Library.)

The organizers maintained a spreadsheet indicating which alumni participated in each dinner. Now they want your help to determine whether any alumnus or alumna took part in all of the dinners.

입력

The input contains several test cases. The first line of a test case contains two integers N and D indicating respectively the number of alumni and the number of dinners organized (1 ≤ N ≤ 100 and 1 ≤ D ≤ 500). Alumni are identified by integers from 1 to N. Each of the next D lines describes the attendees of a dinner, and contains N integers Xi indicating if the alumnus/alumna i attended that dinner (Xi = 1) or not (Xi = 0). The end of input is indicated by N = D = 0.

출력

For each test case in the input your program must produce one line of output, containing either the word ‘yes’, in case there exists at least one alumnus/alumna that attended all dinners, or the word ‘no’ otherwise.

예제 입력 1 복사

3 3
1 1 1
0 1 1
1 1 1
7 2
1 0 1 0 1 0 1
0 1 0 1 0 1 0
0 0

예제 출력 1 복사

yes
no
 

 


풀이 : 처음엔 답이 나왔는데 자꾸 틀렸다고 해서 열 받았다.. 아마 내가 놓친 부분이 있던 거겠지만.. 

시도 1. while문 탈출 조건을 if("0 0".equals(line)) 으로 설정. 

    n , d 가 "0 0"으로 들어온다면? 그래서 땡

시도 2. 그렇다면 line 자체가 없을 때를 찾아야 하나? if(line==null) break; 으로 설정.  

  역시나 n, d가 0으로 들어올 때를 생각했어야 했다. 

 

백준에는 틀린 이유가 나오지 않기 때문에 답답했다. 

그래서 ChatGpt를 이용하기로.. 

 

먼저 문제와 입력 방법을 설명해주고 내가 작성한 코드를 입력한다. 

그리고 왜 틀렸는지 이유를 찾아달라 했더니 내가 초반에 입력했던 코드를 update된 코드라고 말해주는 것이다. 

역시..아직은 학습이 필요하구나 싶어서 내가 생각한 문제점과 반례를 힌트로 줬더니 캐치하는 똑똑한 녀석. 

 

 

결과는? 

 

 

하하.. 

Chat Gpt의 도움을 받아 작성한 코드는 다음과 같다. 

주석도 잘 달아주더라..

 

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

public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String line = br.readLine(); // 한 줄을 읽어온다
        while (line != null) { // 파일이 끝날 때 까지 반복
            StringTokenizer st = new StringTokenizer(line); // line을 토큰으로 분리
            int n = Integer.parseInt(st.nextToken()); // n: 친구의 수
            int d = Integer.parseInt(st.nextToken()); // d: 날짜의 수
            if (n == 0 && d == 0) { // n, d가 모두 0인 경우, 반복문 종료
                break;
            }
            int[] attended = new int[n]; // attended: 각 친구가 참석한 날짜의 수
            for (int i = 0; i < d; i++) {
                st = new StringTokenizer(br.readLine()); // 한 줄을 읽어온다
                for (int j = 0; j < n; j++) {
                    if (Integer.parseInt(st.nextToken()) == 1) { // 해당 친구가 참석한 경우
                        attended[j]++; // 해당 친구의 attended 카운트 증가
                    }
                }
            }
            boolean found = false; // found: 모든 날짜에 참석한 친구가 있는지 여부
            for (int i = 0; i < n; i++) {
                if (attended[i] == d) { // 해당 친구가 모든 날짜에 참석한 경우
                    found = true; // found 여부를 true로 설정
                    break;
                }
            }
            System.out.println(found ? "yes" : "no"); // 모든 날짜에 참석한 친구가 있으면 yes, 없으면 no 출력
            line = br.readLine();

 

 

무서운 AI 세상 

어떻게 살아남지 ? 

728x90