簡體   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