求解一道C语言题
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int GetPrime(int a[], int b[], int n) {
int C = 0;
for(int i = 0; i < n; i++) {
int j;
if(a[i] == 1 || a[i] % 2 == 0)
continue;
for(j = 2; j * j <= a[i]; j++)
if(a[i] % j == 0)
break;
if(j * j > a[i])
b[C++] = a[i];
}
return C;
}
int main() {
int a[20], b[20], n;
srand(time(NULL));
for(int i = 0; i < 20; i++)
a[i] = rand() % 99 + 1;
n = GetPrime(a, b, 20);
printf("原始数组:\n");
for(int i = 0; i < 20; i++)
printf("%d%c", a[i], i % 5 == 4 ? '\n' : ' ');
if(n == 0)
printf("\nNO");
else {
printf("\n素数数组:\n");
for(int i = 0; i < n; i++)
printf("%d%c", b[i], i % 5 == 4 ? '\n' : ' ');
}
putchar('\n');
return 0;
}
广告 您可能关注的内容 |