检查魔方

时间:2013-04-22 19:47:30

标签: c++ magic-square

我有一个程序,它接受一组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;
}

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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;

答案 2 :(得分: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;
}

为了检查,蛮力方式是检查行,列然后分别对角线。