有人知道更有效的方法吗?

时间:2014-03-03 17:44:28

标签: arrays for-loop

我知道这很简单。但这就是困扰我的事情。有一个更好的方法吗? 数组通常不重要,只是这个代码的目标就是我想要压缩的功能。

      for(int p=0; p<28;p++){
                    if(p == 0){
                        Pile1[0] = deck[p];
                    }
                    if(p == 1 || p == 7){
                        if(p == 1){
                            Pile2[0] = deck[p];
                        }
                        if(p == 7){
                            Pile2[1] = deck[p];
                        }
                    }
                    if(p == 2 || p == 8 || p == 13){
                        if(p == 2){
                            Pile3[0] = deck[p];
                        }
                        if(p == 8){
                            Pile3[1] = deck[p];
                        }
                        if(p == 13){
                            Pile3[2] = deck[p];
                        }
                    }
                    if(p == 3 || p == 9 || p == 14 || p == 18){
                        if(p == 3){
                            Pile4[0] = deck[p];
                        }
                        if(p == 9){
                            Pile4[1] = deck[p];
                        }
                        if(p == 14){
                            Pile4[2] = deck[p];
                        }
                        if(p == 18){
                            Pile4[3] = deck[p];
                        }

                    }
                    if(p == 4 || p == 10 || p == 15 || p == 19 || p == 22){
                        if(p == 4){
                            Pile5[0] = deck[p];
                        }
                        if(p == 10){
                            Pile5[1] = deck[p];
                        }
                        if(p == 15){
                            Pile5[2] = deck[p];
                        }
                        if(p == 19){
                            Pile5[3] = deck[p];
                        }
                        if(p == 22){
                            Pile5[4] = deck[p];
                        }                       
                    }
                    if(p == 5 || p == 11 || p == 16 || p == 20 || p == 23 || p == 25){
                        if(p == 5){
                            Pile6[0] = deck[p];
                        }
                        if(p == 11){
                            Pile6[1] = deck[p];
                        }
                        if(p == 16){
                            Pile6[2] = deck[p];
                        }
                        if(p == 20){
                            Pile6[3] = deck[p];
                        }
                        if(p == 23){
                            Pile6[4] = deck[p];
                        }
                        if(p == 25){
                            Pile6[5] = deck[p];
                        }
                    }
                    if(p == 6 || p == 12 || p == 17 || p == 21 || p == 24 || p == 26 || p == 27){
                        if(p == 6){
                            Pile7[0] = deck[p];
                        }
                        if(p == 12){
                            Pile7[1] = deck[p];
                        }
                        if(p == 17){
                            Pile7[2] = deck[p];
                        }
                        if(p == 21){
                            Pile7[3] = deck[p];
                        }
                        if(p == 24){
                            Pile7[4] = deck[p];
                        }
                        if(p == 26){
                            Pile7[5] = deck[p];
                        }
                        if(p == 27){
                            Pile7[6] = deck[p];
                        }
                    }
                }

1 个答案:

答案 0 :(得分:0)

总而言之,你可以摆脱循环。以下代码是等效的。

    Pile1[0] = deck[0];

    Pile2[0] = deck[1];
    Pile2[1] = deck[7];

    Pile3[0] = deck[2];
    Pile3[1] = deck[8];
    Pile3[2] = deck[13];

    Pile4[0] = deck[3];
    Pile4[1] = deck[9];
    Pile4[2] = deck[14];
    Pile4[3] = deck[18];

    Pile5[0] = deck[4];
    Pile5[1] = deck[10];
    Pile5[2] = deck[15];
    Pile5[3] = deck[19];
    Pile5[4] = deck[22];

    Pile6[0] = deck[5];
    Pile6[1] = deck[11];
    Pile6[2] = deck[16];
    Pile6[3] = deck[20];
    Pile6[4] = deck[23];
    Pile6[5] = deck[25];

    Pile7[0] = deck[6];
    Pile7[1] = deck[12];
    Pile7[2] = deck[17];
    Pile7[3] = deck[21];
    Pile7[4] = deck[24];
    Pile7[5] = deck[26];
    Pile7[6] = deck[27];

接下来,不使用Pile1 ... Pile7,2D阵列会更好。所以,只要弄清楚数学和模式。

int Pile[7][7];
for (int p=1; p<=6; p++) {
  for (int i=0, j=p-1; i<p; i++, j+=(7-i)) {
    Pile[p][i] = deck[j];
  }
}

生成相同的结果,其中使用Pile[X]而不是PileX