본문 바로가기
카테고리 없음

백준 #1978 2023-08-23

by 하타라시 2023. 8. 24.

    //개수 입력

    int put = int.Parse(Console.ReadLine());
    string[] data = Console.ReadLine().Split();

    

    //카운트 기록
    int report = 0;
    
    for(int i = 0; i < put; i++) {

        //입력받은 데이터를 int로 형변환
        int count = int.Parse(data[i]);
        

        //count 와 j를 나눈 나머지가 0이면 카운트
        int p = 0;
        for(int j = 1; j <= count; j++) {
            if(count % j == 0)
                p++;
        }

        //p가 2인경우 1과 count가 나왔다는 의미
        if(p == 2) report++;
    }
    //출력
    Console.Write(report);

생각보다 무난했던거같따,,, for문에서 for(int i = 1; i < count / 2; i++) 이런 방식을 했었는데,, 딱히 큰 차이가 없었듯한,,,

아래는 해당 방식으로 구현한 것,,

 

    int put = int.Parse(Console.ReadLine());
    string[] data = Console.ReadLine().Split();
    int report = 0;
    
    for(int i = 0; i < put; i++) {
        int count = int.Parse(data[i]);
        
        int p = 1;
        for(int j = 1; j <= count / 2; j++) {
            if(count % j == 0)
                p++;
        }
        if(p == 2) report++;
    }
    
    Console.Write(report);