繁体   English   中英

代码在不使用 cout 时卡住并且不适用于某些数字

[英]Code gets stuck when not using cout and doesnt work with some numbers

代码的作用:

这段代码的作用是给定所需的素数数量,从 2 开始,它用这些素数组成一个数组。 找到素数它的作用是将一个可能是素数的数字除以该数组上的每个数字,如果每个除法中的余数不为0,则表示这是一个素数,但如果不是余数之一是 0,这意味着它不是素数,所以我们继续搜索它之后的下一个数字。

问题:

1.如果删除,在第46行

cout << ".";

程序卡住了。 这就像薛定谔猫,因为如果我不使用 cout,我不知道它卡在代码的哪一部分,如果我使用 cout,它就可以工作

2.当你给数字,比如1000000,程序退出,代码:-1073741571,这是为什么? 好吧,在第 49 行中,完成了 0/n 的除法; 这里有什么奇怪的? 好吧,当想要的素数为 1000000 和搜索 1000 个第一个素数时为 1000 时,这部分代码在理论上是相同的,至少在找到前 1000 个素数时它应该是相同的

最后,代码:

#include<iostream>

using namespace std;

unsigned int primesWanted;
unsigned int possiblePrime=3; //We are going to start searching on the 3
unsigned int primesFound = 1; //We start asigning 2 because later we declare that we have one prime found, the 2

int main(int argc, char* argv[])
{
    cout <<"Number of primes wanted?:"<< endl << "=========================" << endl;
    cin  >> primesWanted;
    cout << endl << "========================="<< endl;     //the ======== is just for decoration
    
    int primes[primesWanted];                               // this is an array that contains all primes found
    primes[0]= 2;                                           // We asign the 2 as the first prime found
    
    while(primesFound < primesWanted)                       // this will be executed until all primes wanted are found
    {
        cout<< ".";                                         //if you delete this, it doesnt work i dont know why
        for (unsigned int i = 1; i <= primesFound; i++)
        {
            if(possiblePrime % primes[i-1])                 //if the number that we are searching remainder is 0 when dividing by a prime, thats mean that could be a prime
            {
                if(i == primesFound)                        //if i is equal as the number of primes found, that means that that possiblePrime is a prime
                {
                    primes[primesFound] = possiblePrime;    //we add it to the prime list
                    primesFound++;                          //we add one because we have added on prime to the list
                }
            }
            else
            {
                break;                                      //if the number that we are searching remainder is 0 when dividing by a prime, thats mean that is not a prime
            }
        }
        possiblePrime++;                                    //we continue to the next number to search
    }
    
    
    
    
                                                            //we print the primes array
    cout <<endl << "========================="<< endl;
    for (unsigned int i = 0; i < primesFound; i++)
    {
        cout <<primes[i] << ", ";
    }
    cout <<endl << "========================="<< endl;
    
    system("pause");
    return 0;
}

你不能cin >> primesWanted; 因为它是数组的大小,必须是 const。 即使没有cout<<".";代码也能以其他方式完美运行这不可能是问题

您的代码中存在问题,修复后,您的代码可以完美运行!

数组必须有一个const值,这意味着它不能以任何方式依赖于输入。 它必须始终相同。

改变:

int primes[primesWanted];   

至:

int* primes = new int[primesWanted];

毕竟,数组基本上是一个指针,所以这是一回事。 现在,如果我删除cout ,它会完美运行并给出 output :

Number of primes wanted?:
=========================
10

=========================

=========================
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
=========================
Press any key to continue . . .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM