gpt4 book ai didi

c++ - C : Finding the prime numbers from 1 ~ 100.

转载 作者:行者123 更新时间:2023-11-30 21:30:17 25 4
gpt4 key购买 nike

这是我的代码。我觉得逻辑是对的,虽然确实很复杂、很乱、效率很低。但是当我编译它时,它给出了有趣的答案

谁能帮我找出错误所在吗?

#include<stdio.h>
#include<conio.h>
int main(void)
{
int counter = 0;
int i, j;
int c;
int str[100];
int con;
for (i = 1, c = 0; i <= 100; c++, i++)
{
j = 1;
con = 0;
while (i%j == 0)
{
if (j>i)
{
break;
}
printf("i=%d j=%d\n", i, j);
j++;
con++;
printf("The number of con is %d\n", con);
}
if (con <= 2)
{
str[c] = i;
counter++;
}
}
putchar('\n');
printf("The value of counter is %d\n", counter);
for (i = 0; i<counter; i++)
{
printf("%d\n", str[i]);
}
_getch();
return 0;

}

最佳答案

而不是

str[c] = i;

你想拥有

str[counter] = i;

并完全删除c变量。

关于c++ - C : Finding the prime numbers from 1 ~ 100.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26899537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com