我的方法是將每一行的相加, 每一列的相加, 每一個九宮格裡的相加是否等於45
基本上一個數獨的正解 每一行的相加, 每一列的相加, 每一個九宮格裡的相加 都應該為45
如果不等於的話, 我有設一個no變數去做判斷
可是送出後一直有以下錯誤訊息..不知哪裡出錯了...
您的輸出超過測資的輸出!!(line:8)
您額外輸出了: no
可能的原因為
* 累贅的輸出,請勿輸出題目未要求的文字
#include <iostream>
#include <cstdlib>
using namespace std;
int main(void){
const int x = 9; //數獨大小 x*x
int a[x][x], b[x][x];
int total, no;
while (cin){
total = 0;
no = 0;
/* 開始儲存9 * 9陣列的值 */
for (int i = 0; i < x; i++){
for (int j = 0; j < x; j++){
cin >> a[i][j];
}
}
/* 開始檢查每一列是否有重複之值 1+...+9應等於45 */
for (int i = 0; i < x; i++){
for (int j = 0; j < x; j++){
total += a[i][j];
}
if (total != 45){ //若!=45表示有重複之數字
no++;
}
total = 0; //每次檢查完將 total 歸零
}
/* 在將陣列的行列交換 */
for (int i = 0; i < x; i++){
for (int j = 0; j < x; j++){
b[j][i] = a[i][j];
}
}
/* 開始檢查每一行是否有重複之值 1+...+9應等於45 */
for (int i = 0; i < x; i++){
for (int j = 0; j < x; j++){
total += b[i][j];
}
if (total != 45){ //若!=45表示有重複之數字
no++;
}
total = 0; //每次檢查完將 total 歸零
}
/* 開始檢查九宮格裡是否有重複之值 1+...+9應等於45 */
for (int i = 0; i < 9; i+=3){
for (int j = 0; j < 9; j+=3){
for (int x = i; x < i+3; x++){
for (int y = j; y < j+3; y++){
total += a[x][y];
}
}
if (total != 45){ //若!=45表示有重複之數字
no++;
}
total = 0; //每次檢查完將 total 歸零
}
}
/* 最後判斷 no 的值 */
if (no != 0){ //如果no 有值的話表示有動過
cout << "no" << endl;
}else{
cout << "yes" << endl;
}
}
//system("pause");
return 0;
}