#28454: 錯在哪?感謝


cjw931027@gmail.com (noob)

學校 : 北京大学
編號 : 150971
來源 : [1.160.5.51]
最後登入時間 :
2025-05-26 11:31:04

輸出B的部分會錯

#include <iostream>

using namespace std;

int main(){

int data[4];

for(int i=0;i<4;i++){

cin>>data[i];

}

int a;

cin>>a;

int arr[a][4];

for(int i=0;i<a;i++){

for(int j=0;j<4;j++){

cin>>arr[i][j];

}

}

 

 

for(int i=0;i<a;i++){

int x=0,y=0;

for(int j=0;j<4;j++){

if(arr[i][j]==data[j]) x++;

 

else if(arr[i][j]!=arr[i][j-1] && arr[i][j]!=arr[i][j-2] && arr[i][j]!=arr[i][j-3]){

if(arr[i][j]==data[j-1] || arr[i][j]==data[j-2] || arr[i][j]==data[j-3]) y++;

}

}

cout<<x<<'A'<<y<<'B'<<endl;

}

 

return 0;

}

 

#28455: Re:錯在哪?感謝


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [39.12.66.21]
最後登入時間 :
2025-04-20 17:19:22


else if(arr[i][j]!=arr[i][j-1] && arr[i][j]!=arr[i][j-2] && arr[i][j]!=arr[i][j-3]){

if(arr[i][j]==data[j-1] || arr[i][j]==data[j-2] || arr[i][j]==data[j-3]) y++;

}


錯在這裡,我不懂這段程式碼要做什麼,總之不需要arr和arr比較,標準的作法應該是

1. 先把A算出來

2. 算完A再算B

 

#28472: Re:錯在哪?感謝


cjw931027@gmail.com (noob)

學校 : 北京大学
編號 : 150971
來源 : [1.160.5.51]
最後登入時間 :
2025-05-26 11:31:04

#include <iostream>

using namespace std;

int main(){

int ans[4],guess[4],n;

while(cin>>ans[0]>>ans[1]>>ans[2]>>ans[3]){

cin>>n;

for(int i=0;i<n;i++){

int A=0,B=0;

int temp[4];

for(int j=0;j<n;j++) temp[j]=ans[j];

 

cin>>guess[0]>>guess[1]>>guess[2]>>guess[3];

 

for(int j=0;j<4;j++){

if(temp[j]==guess[j]){

A++;

temp[j]=-1;

guess[j]=-1;

}

}

 

for(int j=0;j<4;j++){

if(temp[j]==-1) continue;

for(int k=0;k<4;k++){

if(temp[j]==guess[k]){

B++;

temp[j]=-1;

guess[k]=-1;

break;

}

}

}

cout<<A<<'A'<<B<<'B'<<endl;

}

}

 

return 0;

}

 

記憶體區段錯誤!

Segmentation fault (core dumped)

我後來上網看了影片教學,改成這樣,測試都沒問題但送出卻出現這個
請問怎麼辦



#28477: Re:錯在哪?感謝


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [39.12.66.21]
最後登入時間 :
2025-04-20 17:19:22


for(int j=0;j<n;j++) temp[j]=ans[j];

 

記憶體區段錯誤!

Segmentation fault (core dumped)

我後來上網看了影片教學,改成這樣,測試都沒問題但送出卻出現這個
請問怎麼辦




j<n 改成 j<4

另外要優化IO,不然會TLE