#include<iostream>
using namespace std;
int main()
{
int n,a[1000001];
while(cin>>n)
{
int i,sum1=0,sum2=0,ans=0;
for(i=1;i<=n;i++) cin>>a[i];
for(i=1;i<=n;i+=2) sum1+=a[i];
for(i=2;i<=n;i+=2) sum2+=a[i];
ans=max(sum1,sum2);
cout<<ans<<endl;
}
}
這是陣列太小的問題嗎???\\
該怎麼處理呢???
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
#include
using namespace std;
int main()
{
int n,a[1000001];
while(cin>>n)
{
int i,sum1=0,sum2=0,ans=0;
for(i=1;i<=n;i++) cin>>a[i];
for(i=1;i<=n;i+=2) sum1+=a[i];
for(i=2;i<=n;i+=2) sum2+=a[i];
ans=max(sum1,sum2);
cout<<ans<<endl;
}
}
這是陣列太小的問題嗎???\\
該怎麼處理呢???
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
記憶體區段錯誤! Segmentation fault (core dumped)
宣布在int main內跟外 試看看結果
修改陣列的宣告方式
int * a = new int[1000001]
修改陣列的宣告方式
int * a = new int[1000001]
這是甚麼意思???
小的剛入門看不太懂....