0

これは、大きな配列で頻繁な数を探すための私のコードです。500000で十分使えます。n = 2,000,000でクラッシュする理由を教えてください。

int を long または double に変更しようとしましたが、クラッシュするか、コンパイルできません。

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {

int n=500000;
int A[n];
clock_t begin, end; // this is used for measuring the running time
double elapsed_secs;
int ans;


for (int i=0; i<=0.1*(n-1); i++){
    A[i]=n-i;               
}

for (int i=0.1*n; i<n; i++){
    A[i]=1;  
}


/* Now we run the first algorithm, which is always correct.
 */

begin = clock(); // start couting the time
ans = almostUnanimousAlwaysCorrect(A, n);
end = clock(); // end counting the time
elapsed_secs = 1000*double(end - begin) / CLOCKS_PER_SEC;
cout << endl << "(1) The first algorithm (which is always correct) answers "<<endl;

if (ans==1) cout << "1 (correct)\n";
else  cout << "no frequent number (wrong) \n";

cout << "Time taken for the first algorithm is " << elapsed_secs << " milliseconds." << endl<<endl;


/* Now we run the second algorithm, which is NOT always correct.
   Note that the sorting function change how A looks. So, in principle,
   we should reset A. But let's not worry about that for now.
 */

begin = clock(); // start couting the time
ans = almostUnanimousRandom(A, n);


end = clock(); // end counting the time
elapsed_secs = 1000*double(end - begin) / CLOCKS_PER_SEC;
cout << endl << "(2) The second algorithm (which is NOT always correct) answers "<<endl;
if (ans==1) cout << "1 (correct)\n";
else  cout << "no frequent number (wrong) \n";

cout << "Time taken for the second algorithm is " << elapsed_secs << " milliseconds." << endl<<endl;


system("pause");


}
4

1 に答える 1

6

スタックのメモリが不足しています。代わりにヒープに割り当てます。

int n=500000;
int* A = new int[n];

そして、あなたがそれを終えたら:

delete[] A;
于 2013-05-05T16:30:41.127 に答える