-3

我有一个程序,它接收一组 16 个数字并将它们打印在 4x4 的网格中。然后我必须检查每一行、每一列和对角线加起来的数字是否相同,但是我不能做这部分,因为我不知道它是如何工作的。任何人都可以帮忙吗?

当前代码:

void getNumbers(int numbers[]){
int idx;
for(int x = 0; x < 17; x++){
    cout << "Please enter a number: " << endl;
    cin >> idx;
    numbers[x] = idx;
}
cout << " " << numbers[0] << " " << numbers[1] << " " << numbers[2] << " " << numbers[3] << endl;
cout << " " << numbers[4] << " " << numbers[5] << " " << numbers[6] << " " << numbers[7] << endl;
cout << " " << numbers[8] << " " << numbers[9] << " " << numbers[10] << " " << numbers[11] << endl;
cout << " " << numbers[12] << " " << numbers[13] << " " << numbers[14] << " " <<  numbers[15] << endl;
}
4

3 回答 3

1

如果您看到,请删除任何语法错误,整体代码必须有效!享受 ;-)

bool magicSquare(){
    int idx;
    int numbers[4][4];
    cout << "Please enter your numbers: " << endl;
    for(unsigned int i=0; i<4; i++) {
        for(unsigned int j=0; j<4; j++) {
            cin >> idx;
            numbers[i][j] = idx;
        }
    }
    // Checking 
    for(unsigned int i=0; i<4; i++) {
        int row_sum = 0;
        for(unsigned int j=0; j<4; j++) {
            row_sum+= numbers[i][j];
        }
        int col_sum = 0;
        for(unsigned int j=0; j<4; j++) {
            col_sum+= numbers[j][i];
        }
        int diag_sum_left = numbers[0][0] +  numbers[1][1] +  numbers[2][2];
        int diag_sum_right = numbers[0][2] +  numbers[1][1] +  numbers[2][0];
        if ((col_sum != 15) || 
            (row_sum !=15) ||
            (diag_sum_left != 15) ||
            (diag_sum_right != 15) )
            return false;
    }       
    cout << "Your answer is correct:" << endl;
    for(unsigned int i=0; i<4; i++) {
        for(unsigned int j=0; j<4; j++) 
            cout << numbers[i][j] << "\t";
        cout << endl;
    }       
    return true;
}
于 2013-04-22T20:41:15.520 回答
0

撇开how to check,您在发布的代码中有问题:您提到您有 16 个数字,但实际上您要求的是 17。如果您的numbers数组大小 = 16,那么您将遇到index out of bound错误。

for(int x = 0; x < 17; x++){
            //^^^should be 16, put them into numbers array with numbers[0] to numbers[15]
    cout << "Please enter a number: " << endl;
    cin >> idx;
    numbers[x] = idx;
}

对于检查,蛮力方法是分别检查行、列然后对角线。

于 2013-04-22T20:04:28.143 回答
0

你可以直接写下所有的总和和规则

像这样:

int horizontal1 = numbers[0]+ numbers[1]+ numbers[2]+ numbers[3];
int horizontal2 = numbers[4]+ numbers[5]+ numbers[6]+ numbers[7];
int horizontal3 = numbers[8]+ numbers[9]+ numbers[10]+numbers[11];
int horizontal4 = numbers[12]+numbers[13]+numbers[14]+numbers[15];

int vertical1 = numbers[0]+numbers[4]+numbers[8]+ numbers[12];
int vertical2 = numbers[1]+numbers[5]+numbers[9]+ numbers[13];
int vertical3 = numbers[2]+numbers[6]+numbers[10]+numbers[14];
int vertical4 = numbers[3]+numbers[7]+numbers[11]+numbers[15];

int diagonal1 = numbers[0]+numbers[5]+numbers[10]+numbers[15];
int diagonal2 = numbers[3]+numbers[6]+numbers[9]+ numbers[12];

// The result of check would be in this variable
bool result = horizontal1 == horizontal2 && 
              horizontal1 == horizontal3 &&
              horizontal1 == horizontal4 &&
              horizontal1 == vertical1 &&
              horizontal1 == vertical2 &&
              horizontal1 == vertical3 &&
              horizontal1 == vertical4 &&
              horizontal1 == diagonal1 &&
              horizontal1 == diagonal2;
于 2013-04-22T19:55:25.410 回答