c - for循环一定有错误

标签 c

For some reason this cycle doesn't stop, without the else it doesn't even print the first line and I dont't know how to fix it

    for(int i = 0; prime <= sqrt(rest); ++i) {
        printf("%d", i);
        if (primes[i] == 1) {
            prime = primes[i];
            printf("%d", prime);
            while (rest % prime == 0) {
                rest = rest / prime;
                rep++;
            }
            if (rep == 1) {
                printf("%d", i);
                print++;
            } else if (rep > 1) {
                printf("%d^%d", prime, rep);
                print++;
            }
            if (rest != 1 && print != 0) {
                printf(" x ");
            } else if (print != 0) {
                printf("\n");
            }
            rep = 0;
            print = 0;
        }else{
            printf("WTF\n%d\n", i);
            i++;
        }
    }
    if (rest > 1) {
        printf("%lld\n", rest);
    } else{
        printf("WTF\n");
        break;
    }
}

最佳答案

你永远不会改变素数的值,这就是你的条件被设置为停止的原因,所以如果它永远不会改变,它就永远不会停止。也许你的意思是在 for 循环开始时是 prime 而不是 i ?

关于c - for循环一定有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47191606/

相关文章:

看不懂这个圆周率算法

c - 在 C 中同步查询 SQLite 数据库

c - 是否有可能在 c 中有一个数组 union

c - 数据请求仅返回地址值(I2C)

c - 在编译时避免变长堆栈数组

c - 如何减少我的 C 程序的开销?

android - 如何使内联函数在可执行文件中只占用一份内存空间

c++ - abs(unsigned long) 有什么意义吗?

javascript - C 中 JavaScript 的等效 call() apply() 是什么?

c - C中的通用数据结构