2014-11-06 82 views
-3

問題陳述鏈接SIGSEGV錯誤: - http://www.hackerearth.com/problem/algorithm/range-query/運行 - 在下面的代碼

我的解決辦法: -

#include <iostream> 
using namespace std; 

int main() 
{ 
    long long n=0,q=0,x=0,y=0,val=0; //initialize variables 
    int a[1000]; 
    int counter=0; 
    cin >>n ; 
    for (int i=0;i<n;i++) 
    { 
     cin >>a[i]; //populate the array 
    } 
    cin >>q; 

    for (int k=0;k<q;k++) 
    { 
     cin >>x >>y >>val; 
      for (int j=x;j<=y;j++) 
      { 
        if (a[j]==val) 
         counter++; 

      } 
    cout <<counter << "\n"; 
    x=0; 
    y=0; 
    val=0; 
    counter=0; 
    a[10]=0; 
    } 

    return 0; 
} 

錯誤: - 在執行上面的代碼 SIGSEGV時遇到錯誤。

回答

3

N是10^4的問題,你的最大的數組大小爲1000

int a[1000];//Make this 10000 
+0

謝謝,工作就好了! – user2295715 2014-11-06 18:18:04