본문 바로가기

Algorithm/BOJ 기초

[백준] 소수찾기 1978 Python C++

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
tc = int(input())
inputs = list(map(int, input().strip().split()))
cnt = 0
 
for i in inputs:
    nop = 0
    if i>=2:
        if i == 2:
            cnt +=1
        else:
            for j in range(2,i):
                if i%j == 0:
                    nop = 1
                    break
            if nop == 0:
                cnt +=1
print(cnt)
cs

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
 
using namespace std;
    
int main(){
    
    int n;
    int tc;
    cin >> tc;
    int cnt = 0;
    int check;
 
    for (int i = 0; i < tc; i++){
        check = 0;
        scanf("%d"&n);
        if (n>=2){
            if (n == 2)
                cnt += 1;
            else{
                for (int j = 2; j < n; j++){
                    if (n%j == 0){
                        check = 1;
                        break;
                    }
                }
                if (check == 0){
                    cnt += 1;
                }
            }
        }
    }
    printf("%d\n", cnt);
 
    return 0;
}
cs