#include <stdio.h>
int main(){
int num[9][9], row, column, i, j, k, l, temp, check, min[3] = {0, 3, 6}, max[3] = {3, 6, 9};
for (row = 0; row < 9; row++){
for (column = 0; column < 9; column++){
do{
check = 0;
printf("Input the number at (%d,%d):", row + 1, column + 1);
scanf("%d", &num[row][column]);
temp = num[row][column];
if (num[row][column] < 0 || num[row][column] > 9){
printf("Input Error!\n");
check = 1;}
for (i = 0; i < column; i++)
if (temp == num[row][ i]){
printf("Same number on the same column!\n");
check = 1;}
for (i = 0; i < row; i++)
if (temp == num[ i][column]){
printf("Same number on the same row!\n");
check = 1;}
if (row < 3)
i = 0;
else if (row < 6)
i = 1;
else
i = 2;
if (column < 3)
j = 0;
else if (column < 6)
j = 1;
else
j = 2;
for (k = min[ i]; k < max[ i]; k++)
for (l = min[j]; l < max[j]; l++)
if (k != row || l != column)
if (temp == num[k][l]){
printf("Same number in the same square!\n");
check = 1;}}
while(check);}}
if (!check)
printf("You win!\n");
for (row = 0; row < 9;row++){
for (column = 0; column < 9; column++)
printf("%4d", num[row][column]);
printf("\n");}
system("Pause");
return 0;}
2 6 5 1 7 3 4 8 9
1 8 3 5 4 9 7 2 6
7 9 4 2 8 6 5 1 3
4 1 8 3 9 7 2 6 5
9 2 7 6 5 8 3 4 1
5 3 6 4 1 2 9 7 8
8 5 1 7 3 4 6 9 2
3 4 2 9 6 1 8 5 7
6 7 9 8 2 5 1 3 4
if (row < 3)
i = 0;
else if (row < 6)
i = 1;
else
i = 2;
if (column < 3)
j = 0;
else if (column < 6)
j = 1;
else
j = 2;
for (k = min[ i]; k < max[ i]; k++)
for (l = min[j]; l < max[j]; l++)
if (k != row || l != column)
if (temp == num[k][l]){
printf("Same number in the same square!\n");
check = 1;}
原帖由 EnTaSIa 於 26/4/2008 08:47 PM 發表
感覺上for (k = min; k < max; k++)好像應該是for (k = min; k < max; k++){其實沒完全搞明白,但不平衡感令我不得不說-__-}
原帖由 EnTaSIa 於 27/4/2008 09:10 AM 發表
可能是min和max的array數值重疊的錯
試將max[3] = {4,7,10} ?
(我沒讀開C++...)
試轉作pascal代碼後
似乎是你的maximum value 3,6,9全部都應該改作4,7,10
因為你用的是 ?
原帖由 RX78-2 於 4/5/2008 09:44 PM 發表
你懂怎麼轉啊...
真強...
可否說一說你在check square時是用甚麼方法的...
例如說是不是逐行check還是用甚麼方法...
事實上本是Pascal的人很難看懂...
原帖由 魔術師Chikorita 於 5/5/2008 01:00 AM 發表
嗯,只顧及了輸入數值本身而沒理會之後的
但不解為可只在特定數值出現問題(學校試的是"3"出error、手指試的是"1"出error)
那個minor mistake 是交換了"Same number in the same row!"和"......column!"的位置﹖
歡迎光臨 香港寵物小精靈村落 論壇 (https://proxy.archiver.hkpnve.pokebeacon.com/) | Powered by Discuz! X3.2 |