题解 | #牛牛的素数判断#
牛牛的素数判断
https://www.nowcoder.com/practice/81c8387f72824cceb5c955e3dbfd2882
#include <stdio.h> #include <stdbool.h> bool is_prime(int n); int main() { int n = 0; while (1 == scanf("%d", &n)) { int num = 0; for (int i = 0; i < n; i++) { scanf("%d", &num); if (is_prime(num)) { printf("true\n"); } else { printf("false\n"); } } } return 0; } bool is_prime(int n) { int flag = 1; int i = 0; for (i = 2; i < n; i++) { if (0 == (n % i)) { flag = 0; break; } } if (i == n && 1 == flag) { return true; } else { return false; } }