我有一个试图找到魔方的程序.一个数字的方形矩阵,其中所有行,列,对角线加起来相同的数字.
到目前为止,我有一个3x3阵列,成功填充真正的随机数.这样做似乎工作正常,但是当我将程序包含在while(true)
循环中时,程序会永远运行而不会找到魔方,我认为这是因为我的条件语句写得不正确.这是:
if (row0 == row1 == row2 == col0 == col1 == col2 == dia1 == dia2) {
cout << "We have a magic square!" << endl;
cout << troysArray[i][j];
cout << "";
break;
}
Run Code Online (Sandbox Code Playgroud)
整个计划在这里:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//Defining the sum function, which takes 3 integers as arguments.
int addnums(int x,int y,int z){
int result = x + y + z;
return result;}
int main() {
srand(time(0));
//Initial array contents …
Run Code Online (Sandbox Code Playgroud)