我用C++寫的,在Codeblocks上運行,輸入兩次問題,都可得到正確解
但系統顯示出以下訊息:
WA (line:2)
答案不正確
您的答案為: no 正確答案為: yes
想請教大家,程式的哪一個地方出問題了,謝謝。
---------------------以下為程式碼-------------------
#include <iostream>
#define max 9
#define ans 405
using namespace std;
int w=0,x=0,y=0,z=0;
int w1=0,x1=0,y1=0,z1=0;
int N1[10]={0},N2[10]={0};
int num1=0,num2=0;
int answer=1,answer1=1,answer2=1;
int i=0,j=0;
char m[10][10];
int n[10][10];
int CtN(char ch); //字串轉數字
int main()
{
//input the matrix
while(cin.peek()){
N1[num1]=0,N2[num2]=0; //清除陣列中加總的值
//輸入九宮格
for(w=0;w<max;w++){
for(x=0;x<max;x++){
cin >> m[w][x];
n[w][x] = CtN(m[w][x]);
}
}
//sum
for(w1=0;w1<max;w1++){
for(x1=0;x1<max;x1++){
N1[num1]+= n[w1][x1];
N2[num2]+= n[x1][w1];
}
}
//judge the sum is correct
if(N1[num1]!=ans){
cout << "N1[num1]" << N1[num1] << endl;
//num1++;
answer = 0;
break;
}
else{
answer = 1;
}
if(N2[num2]!=ans){
cout << "N2[num2]" << N2[num2] << endl;
//num2++;
answer1 = 0;
break;
}
else{
answer1 = 1;
}
i=0;
while ( i < 9 ){
j = 0;
while ( j < 9 ) {
if( ( n[i][j] + n[i+1][j] + n[i+2][j] + n[i][j+1] + n[i+1][j+1] + n[i+2][j+1] +
n[i][j+2] + n[i+1][j+2] + n[i+2][j+2] ) != 45 ){
answer2 = 0;
break;
}
j += 3;
}
i += 3;
}
if(answer == 0 || answer1 == 0 || answer2 == 0){
cout << "no" << endl;
}
else cout << "yes" << endl;
}
return 0;
}
int CtN(char ch)
{
int num;
num = int(ch)-48;
//cout << "num = " << num << endl;
return num;
}