L1-028. Judge prime number, l1-028 judge Prime Number
The objective of this question is to determine whether a given positive integer is a prime number.
Input Format:
The input returns a positive integer N (<= 10) in the first row, followed by N rows. Each row returns a positive integer smaller than 231.
Output Format:
For each positive integer to be judged, if it is a prime number, "Yes" is output in a row; otherwise, "No" is output ".
Input example:
211111
Output example:
YesNo
Time Limit: 400 ms memory limit: 65536 kB code length limit: 8000 B Judgment program Standard author Chen Yue Note: (1) the number of questions required to be input is a positive integer, so we need to consider the case of 1, 1 is not a prime number; (2) when determining whether a number is a prime number, you only need to traverse from 2 to the square root of the number. If there is a fully divided number, you can exit the loop. This reduces the time complexity.
#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){ int n; int i, j; int arr[10]; int flog = 0; scanf("%d", &n); if(n > 10 || n < 0) exit(0); for(i = 0; i < n; i++) { scanf("%d", &arr[i]); } for(i = 0; i < n; i++) { if(arr[i] == 1) printf("No\n"); else{ flog = 0; for(j = 2; j <= sqrt(arr[i]); j++) { if(arr[i]%j == 0) { flog = 1; break; } } if(flog == 1) printf("No\n"); else printf("Yes\n"); } } return 0;}