C++ - 这个条件语句写得正确吗?还是有更有效的写法?
C++ - Is this conditional statement written correctly? Or is there a more effective way to write it?
我有一个程序试图找到一个魔方。数字的方阵,其中所有行,列,对角线加起来都是相同的数字。
到目前为止,我有一个 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;
}
整个程序在这里:
#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 before random numbers are substituted
int troysArray[3][3] = {
{1,3,2},
{4,6,5},
{7,9,8},
};
int i;
int j;
int row0;
int zero_zero;
int zero_one;
int zero_two;
int row1;
int one_zero;
int one_one;
int one_two;
int row2;
int two_zero;
int two_one;
int two_two;
int col0;
int col1;
int col2;
int dia1;
int dia2;
int sum = row0;
while(true) {
for (i = 0;i < 3;i++){
for (j = 0;j < 3;j++){
//Generating random numbers between 1-9, with which to populate troysArray.
troysArray[i][j] = 1 + (rand() % 9);
cout << troysArray[i][j];
cout << "";
//If all the rows,columns, and diagonals are equal,we have a magic square!
if (row1 == sum && row2 == sum && col0 == sum && col1 == sum && col2 == sum &&
dia1 == sum && dia2 == sum) {
cout << "We have a magic square!" << endl;
cout << troysArray[i][j];
cout << "";
break;}
}
cout << endl;
}
}
//Adding up row 0 (top row):
zero_zero = troysArray[0][0];
zero_one = troysArray[0][1];
zero_two = troysArray[0][2];
row0 = addnums(zero_zero,zero_one,zero_two);
cout << "The sum of row 0 equals: " << zero_zero + zero_one + zero_two << endl;
//Adding up row 1 (middle row):
one_zero = troysArray[1][0];
one_one = troysArray[1][1];
one_two = troysArray[1][2];
row1 = addnums(one_zero,one_one,one_two);
cout << "The sum of row 1 equals: " << one_zero + one_one + one_two << endl;
//Adding up row 2 (bottom row):
two_zero = troysArray[2][0];
two_one = troysArray[2][1];
two_two = troysArray[2][2];
row2 = addnums(two_zero,two_one,two_two);
cout << "The sum of row 2 equals: " << two_zero + two_one + two_two << endl;
cout << "n";
//Adding up col 0 (Left):
col0 = addnums(zero_zero,one_zero,two_zero);
cout << "The sum of col 0 equals: " << zero_zero + one_zero + two_zero << endl;
//Adding up col 1 (Middle):
col1 = addnums(zero_one,one_one,two_one);
cout << "The sum of col 1 equals: " << zero_one + one_one + two_one << endl;
//Adding up col 2 (Right):
col2 = addnums(zero_two,one_two,two_two);
cout << "The sum of col 2 equals: " << zero_two + one_two + two_two << endl;
cout << "n";
//Adding up tL-bR diagonal (dia 1):
dia1 = addnums(zero_zero,one_one,two_two);
cout << "The sum of dia 1 equals: " << zero_zero + one_one + two_two << endl;
//Adding up bL-tR diagonal (dia 2):
dia2 = addnums(two_zero,one_one,zero_two);
cout << "The sum of dia 2 equals: " << zero_two + one_one + two_zero << endl;
return 0;
}
相等运算符在 C 中不起作用,并且C++您认为的方式。
它是从左到右评估的,因此a == b == c
变得(a == b) == c
或(true/false) == c
。
解决此问题的正确方法是单独比较每个值:
int sum = row0;
if (row1 == sum && row2 == sum && ...) { ... }
更新:
您可以像这样创建自己的多值比较器:
template <class Arg, class... Args>
bool all_equal(Arg&& arg, Args&&... args) {
static_assert(sizeof...(args), "At least 2 arguments expected");
return (arg == args && ...);
}
// Example
if (all_equal(row0, row1, ...)) {}
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 欧拉项目#8答案是大以获得有效答案
- 在没有太多条件句的情况下,我如何避免被零除
- 基于多个条件处理地图中的所有元素
- 调整大小后指向元素值的指针unordered_map有效?
- 在循环条件 i<sqrtN(预先计算)和 i*i<N 中,C++哪个更有效?
- C++ - 这个条件语句写得正确吗?还是有更有效的写法?
- 一个有效的图算法,满足以下条件
- C ,最有效的方法将大写速度更改为小写,反之亦然,而无需条件分支
- 类模板的成员函数有条件无效(隐式实例化有效;显式实例化失败)
- 编译时调度:以有效调用为条件
- 插入最有效的数据结构,然后使用不同的条件进行排序
- C++ - 按照特定条件对双向量进行分组的有效方法
- 如何有效地设计一种方法,使每个条件都依赖于另一个条件
- 将数组用作c++中有效的条件表达式
- 评估IF条件的最有效方法是什么?
- 为什么检查分配操作的结果有效,但不能与其他条件相结合
- 在预处理条件中使用布尔字面值是否有效?
- C++ -- 如何从 STL 容器中删除具有这种有效条件的元素
- c++有效的缓冲文本和有条件地将部分写入文本文件的方法