Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

1.求解:下面这个逻辑上出错误了吗?运行的时候直接崩溃了。。 循环这个东西看着不怎么难,可是每当自己要开始嵌套组合的时候就感觉十分绕啊。。 求帮助啊!

include <stdio.h>

int main ()
{

int i;            //被除数; 
int x;            //从1开始跑数字; 
int cnt = 0;
int isPrime = 1;
int n;            //用户输入第几个素数; 
printf("input whatever you want :
");
scanf("%d", n);

while (n!=cnt)
{
    for (x = 1; x < 99999; x ++)
    {
        for (i = 2; i < x; i++)
        {
            if (x%i == 0)
            {
                isPrime = 0;
                break;
             } 
        }
        if(isPrime == 1)
        {
            cnt++;
        }
     }
}
printf("%d
", x);

return 0;

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
287 views
Welcome To Ask or Share your Answers For Others

1 Answer

如楼上所说一定要多用抽象,main函数里最好只有函数的调用,其他的逻辑实现放到另外的函数中。
再者请不要忽略编译器的warnning. 你的scanf这样是有问题的。

还有就是学会 GDB之类的单步调试。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...