#30997: 不知道是什麼原因造成記憶體區段錯誤呢...:(


jamie0118 (knownothing)

學校 : 不指定學校
編號 : 192009
來源 : [140.114.199.55]
最後登入時間 :
2022-12-19 20:42:46
b938. kevin 愛殺殺 | From: [111.254.217.24] | 發表日期 : 2022-07-01 11:14

#include<bits/stdc++.h>
using namespace std;
struct node{
    int num;
    bool alive=true;
    struct node *next;
};
int kill(int the_num,struct node people[]){
    //the_num-1是被叫到的人的位置 
    int the_kill=people[the_num-1].next->num;
    people[the_num-1].next->alive=false;
    people[the_num-1].next=people[the_num-1].next->next; 
    return the_kill;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,m,i;
    cin>>n>>m;
    struct node people[n];
    for(i=0;i<n-1;++i){
        people[i].num=i+1;
        people[i].next=&people[i+1];
    }
    people[n-1].num=n;
    people[n-1].next=nullptr;
    int the_num;
    while(m--){
        cin>>the_num; //num=the_num的位置在the_num-1 
        if(the_num>n) continue;
        if(people[the_num-1].alive==false || people[the_num-1].next==nullptr) cout<<"0u0 ...... ?"<<'\n';
        else cout<<kill(the_num,people)<<'\n';
    } 
    return 0;

}

目前試17、18、19出現RE

如果說是因為超出陣列長度....我有試著改成宣告struct node people[n+100] 但沒效就是了(不知道是不是這樣修正啦哈哈)

望諸位指點迷津...謝謝大阿:)

 
#31079: Re: 不知道是什麼原因造成記憶體區段錯誤呢...:(


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
b938. kevin 愛殺殺 | From: [39.9.78.186] | 發表日期 : 2022-07-10 09:02


    struct node people[n];


大概是stack overflow,我改成動態記憶體或是vector就AC了

 
#31136: Re: 不知道是什麼原因造成記憶體區段錯誤呢...:(


jamie0118 (knownothing)

學校 : 不指定學校
編號 : 192009
來源 : [140.114.199.55]
最後登入時間 :
2022-12-19 20:42:46
b938. kevin 愛殺殺 | From: [114.27.175.142] | 發表日期 : 2022-07-14 10:21


    struct node people[n];


大概是stack overflow,我改成動態記憶體或是vector就AC了


喔天啊謝謝你!我過了!!!!(ノ◕ヮ◕)ノ*:・゚✧

 
ZeroJudge Forum