본문 바로가기

Programming/Algorithm

[C++] 백준 1978번 : 소수 찾기


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





#include 
#include 
using namespace std;
int main() {
	cin.tie(NULL);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	int *ary = new int[n];

	for (int i = 0; i < n; i++)
		cin >> ary[i];

	int cnt = 0;
	int flag;
	for (int i = 0; i < n; i++) {
		flag = 1;
		for (int j = 2; j <= sqrt(ary[i]); j++) {
			if (ary[i] % j == 0)
				flag=0;
		}
		if (ary[i] == 1)
			cnt--;
		if (flag == 1)
			cnt++;
	}
	cout << cnt << "\n";
}