我对搜索 1 到 1000 之间的所有质数感到困惑。我正在尝试这样做并每行打印 13 个。我的代码适用于前 5 个数字,然后就结束了。到目前为止,这是我所拥有的,我当前的输出是 12357。有什么问题吗?提前致谢!
void removenonprimes(int d, int ary[]) {
int i;
int h = 0;
for (i = 0; i < sizeof(ary); i++) {
h = ary[i];
if (h % d == 0 || h == d) {
ary[i] = 0;
}
}
}
void prnt(int ary[]) {
int i;
int holder;
int count = 0;
for (i = 0; i < sizeof(ary); i++) {
if (i > 1) {
removenonprimes(i, ary);
holder = ary[i];
//case for when number is not 13th in its line
if (holder != 0 && count != 13) {
printf("%d", holder);
count++;
}
//case for when number is 13th in its line
else if (holder != 0 && count == 13) {
printf("%d\n", holder);
count = 0;
}
} else {
printf(" %d", ary[i]);
}
}
}
最佳答案
您无法检查 sizeof(ary)
,因为 sizeof(ary)
返回数组指针的大小,而不是数组的大小。
您需要将大小 (1000) 作为参数传递给您的函数,或者使用全局常量。
void prnt(int ary[], arySize){
...
for(i = 0; i < ary; i++){
并称它为
prnt(nums, 1000);
您需要为 removenonprimes 函数做类似的事情。
关于c - 在 C 中搜索素数时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22676696/