백준 1978번

김경욱·2025년 8월 6일

백준

목록 보기
24/121

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

    int number = in.nextInt();
    in.nextLine();

    String text = in.nextLine();
    String[] word = text.split(" ");

    int count = 0;

    for (int i = 0 ; i < number; i++)
    {

        int total =0;

        int num = Integer.parseInt(word[i]);   //int num = 1 , 3, 5, 7


        for (int j=1; j <= num; j++)
        {
            if (num % j == 0 )
            {
                total += j;     // total = 4
               

            }


        }
        
        if (total == num+1 && num != 1)
        {
            count++;
        }

    }
    System.out.println(count);

   

    




    }

}

코드를 맨 처음에 짤때는 쉽다고 생각했는데 시간이 지날수록 어려워졌다. 나의 가장 큰 실수는 if (total == num+1 && num != 1)
{
count++;
} total을 다 구한 후에 if문에 넣은 것이 아니라 total을 구하는 중에 if문을 넣어 만약 어떠한 수가 약수를 더하는중에 total+1의 값과 같아진다면 오류가 생기는 상황을 야기할 수 있다는 것이다. 처음에는 뭐가 오류인지 몰랐는데 지피티한테 예시를 들어달라고 하니까 충분히 오류가 생기는게 이해가 됐다.

0개의 댓글