#include <bits/stdc .h>
using namespace std ;
int main () {
int n ;
cin >> n ;
int a [n] ;
for ( int i = 0 ; i < n ; i ) {
cin >> a [i] ;
}
const int N = 1e6 2 ; // This is where the problem is...
bool check [N] ;
for ( int i = 0 ; i < N ; i ) {
check [i] == 0 ;
}
for ( int i = 0 ; i < N ; i ) {
if ( a [i] >= 0 ) {
check [a[i]] = 1 ;
}
}
int ans = -1 ;
for ( int i = 0 ; i < N ; i ) {
if ( check [i]==0 ) {
ans = i ;
break;
}
}
cout << ans ;
return 0 ;
}
當我在 VS Code 終端中運行上述程式時,我沒有得到任何輸出...:
PS C:\Users\LENOVO\Desktop\C Programs> cd "c:\Users\LENOVO\Desktop\C Programs\" ; if ($?) { g smallest_positive_missing_number.cpp -o smallest_positive_missing_number } ; if ($?) { .\smallest_positive_missing_number }
6
0 -9 1 3 -4 5
PS C:\Users\LENOVO\Desktop\C Programs>
uj5u.com熱心網友回復:
首先,停止使用 VLA,因為 C 標準不支持它。請改用 vector 或 new。
int *a = new int[n];
bool *check = new bool[N];
并像使用普通陣列一樣使用它們。
至于你的問題,很明顯是大VLA引起的,切換到new或者vector就完美運行了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/381986.html
