返回列表 發帖
本帖最後由 b1081081 於 2010-11-14 10:35 編輯

基於對社會大眾有著愛民愛己的心,在此破例破解士豪的程式碼給大家
不要說我無情 倒是可以說我白目(ㄏㄏ)
對於士豪的刪除程式碼的行為 對我來說有跟沒有一樣= =(好囂張)
程式碼如下:
  1. /* 判斷一個九宮格數字是不是一個數獨的正解 */
  2. #include <iostream>
  3. #include <cstdlib>
  4. using namespace std;
  5. int main(void){
  6.     const int x = 9; //數獨大小 x*x
  7.     int a[x][x], b[x][x];
  8.     int total, no;
  9.     while (true){
  10.           total = 0;
  11.           no = 0;
  12.           /* 開始儲存9 * 9陣列的值 */
  13.           for (int i = 0; i < x; i++){
  14.               for (int j = 0; j < x; j++){
  15.                   if (cin >> a[i][j])
  16.                   ;
  17.                   else
  18.                       goto END;
  19.               }
  20.           }
  21.           /* 開始檢查每一列是否有重複之值 1+...+9應等於45 */
  22.           for (int i = 0; i < x; i++){
  23.               for (int j = 0; j < x; j++){
  24.                   total += a[i][j];
  25.               }
  26.               if (total != 45){ //若!=45表示有重複之數字
  27.                  no++;
  28.               }
  29.               total = 0; //每次檢查完將 total 歸零
  30.           }

  31.           /* 在將陣列的行列交換 */
  32.           for (int i = 0; i < x; i++){
  33.               for (int j = 0; j < x; j++){
  34.                   b[j][i] = a[i][j];
  35.               }
  36.           }
  37.          
  38.           /* 開始檢查每一行是否有重複之值 1+...+9應等於45 */
  39.           for (int i = 0; i < x; i++){
  40.               for (int j = 0; j < x; j++){
  41.                   total += b[i][j];
  42.               }
  43.               if (total != 45){ //若!=45表示有重複之數字
  44.                  no++;
  45.               }
  46.               total = 0; //每次檢查完將 total 歸零
  47.           }

  48.           /* 開始檢查九宮格裡是否有重複之值 1+...+9應等於45 */
  49.           for (int i = 0; i < 9; i+=3){
  50.               for (int j = 0; j < 9; j+=3){
  51.                   for (int x = i; x < i+3; x++){
  52.                       for (int y = j; y < j+3; y++){
  53.                           total += a[x][y];
  54.                       }
  55.                   }
  56.                   if(total != 45){
  57.                       no++;
  58.                   }
  59.                   total = 0; //每次檢查完將 total 歸零
  60.               }   
  61.           }
  62.           /* 最後判斷 no 的值 */         
  63.           if (no > 0){
  64.               cout << "no" << endl;      
  65.           }else{
  66.               cout << "yes" << endl;   
  67.           }
  68.     }
  69.     //system("pause");
  70. END:
  71.     return 0;
  72. }
複製代碼
但是本人認為寫的不甚完美 所以寫了一個更精簡的版本 並把理由寫在註解的地方
程式碼如下:
  1. /* 判斷一個九宮格數字是不是一個數獨的正解 */
  2. #include <iostream>
  3. #include <cstdlib>
  4. using namespace std;
  5. int main(void){
  6.     const int x = 9; //數獨大小 x*x
  7.     int a[x][x];//這裡並不需要用到兩個陣列 原因 請自行看第19~29行
  8.     int total,total2, no;
  9.     while (true){
  10.           total = 0; total2 = 0;//多用一個的原因 請自行看第19~29行
  11.           no = 0;
  12.           /* 這裡並不需要用到IF,士豪多用了 */
  13.           for (int i = 0; i < x; i++){
  14.               for (int j = 0; j < x; j++){
  15.                   cin >> a[i][j];
  16.               }
  17.           }
  18.           /* 沒有必要多用四個FOR迴圈 兩個就可以搞定了 */
  19.           for (int i = 0; i < x; i++){
  20.               for (int j = 0; j < x; j++){
  21.                   total += a[i][j];
  22.                   total2 += a[j][i];
  23.               }
  24.               if (total != 45 || total2 != 45){ //若!=45表示有重複之數字
  25.                  no++;
  26.               }
  27.               total = 0; //每次檢查完將 total 歸零
  28.               total2 = 0;
  29.           }
  30.           /* 這裡寫的很好 我原本也要這樣寫 */
  31.           for (int i = 0; i < 9; i+=3){
  32.               for (int j = 0; j < 9; j+=3){
  33.                   for (int x = i; x < i+3; x++){
  34.                       for (int y = j; y < j+3; y++){
  35.                           total += a[x][y];
  36.                       }
  37.                   }
  38.                   if(total != 45){
  39.                       no++;
  40.                   }
  41.                   total = 0; //每次檢查完將 total 歸零
  42.               }   
  43.           }
  44.           /* 這裡OK */         
  45.           if (no > 0){
  46.               cout << "no" << endl;      
  47.           }else{
  48.               cout << "yes" << endl;   
  49.           }
  50.     }
  51.     //system("pause");
  52.     //沒必要用到 goto 更難看而已
  53.     return 0;
  54. }
複製代碼
OFCOURS 本人當然有跑過 確定無誤(廢話)
大家自行比較吧!!
離離草原上
一歲一枯榮
野火燒不盡
春風吹又生

TOP

破解別人的程式碼算不算犯法阿老師??
離離草原上
一歲一枯榮
野火燒不盡
春風吹又生

TOP

返回列表