| 這個program的效果是確認在9*9的方格中,在同一個橫排、直排和3*3的方格中的數值沒有相同﹕
#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 變數num[1][7]將以"Same number in the same square!"的理由被系統拒絕。 事後證明num[ i][j](i = 0-1, j = 3-8; i = 3-5, j = 3-5)也會因以上理由而不接納"2"的輸入。 (num[5][6]以後的變數難以證明) 認為用來確認3*3方格內數值的部分﹕
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;} 有誤,請問各位有關問題的錯誤原因和修正方法。 [ 本帖最後由 魔術師Chikorita 於 26/4/2008 10:52 PM 編輯 ] |
|