#6271: 新手一個,請各位幫幫忙


jason21716 (阿鴻)

學校 : 國立成功大學
編號 : 13895
來源 : [118.166.141.201]
最後登入時間 :
2020-04-25 01:04:43
c094. 00661 - Blowing Fuses -- UVa661 | From: [111.240.97.157] | 發表日期 : 2012-02-01 22:12

各位好,我的程式碼可以跑得過範例

但是送出時卻出現RE(記憶體區段錯誤)

可以請各位幫幫我嗎? 

這是我的程式碼:

#include<iostream>
using namespace std;
int main() {
    int n,m,c,count=0;
    while(cin>>n>>m>>c){
        if(n==0)
            break;
        count++;
        cout<<"Sequence "<<count<<endl;
        int power[n+1],on[n+1],temp,total,max=0;
        for(int i=1;i<=n;i++)
            cin>>power[i];
        for(int i=1;i<=n;i++)
            on[i]=0;
        for(int i=1;i<=m;i++){
            total=0;
            cin>>temp;
            if(on[temp]==0)
                on[temp]=1;
            else
                on[temp]=0;
            for(int j=1;j<=n;j++)
                total+=power[j]*on[j];
            if(max<=total)
                max=total;
            if(total>c){
                cout<<"Fuse was blown.\n\n";
                break;
                }
            }
        if(total<=c)
            cout<<"Fuse was not blown.\nMaximal power consumption was "<<max<<" amperes.\n\n";
        }
    return 0;
}

 
ZeroJudge Forum