简体   繁体   English

魔方 3x3 C++

[英]Magic Square 3x3 c++

our teacher told us to make a c++ program that generates a magic square 3x3 or higher and this is what i've gotten so far but its not working.我们的老师告诉我们制作一个 C++ 程序,生成一个 3x3 或更大的幻方,这是我到目前为止所得到的,但它不起作用。 help me please.请帮帮我。

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
// A  B  C
// D  E  F
// G  H  I
int main()
{
srand(time(0));
int A,B,C,D,E,F,G,H,I;
do
{A=rand()%9+1;
B=rand()%9+1;
C=rand()%9+1;
D=rand()%9+1;
E=rand()%9+1;
F=rand()%9+1;
G=rand()%9+1;
H=rand()%9+1;
I=rand()%9+1;}
while ((A+B+C!=15) && (D+E+F!=15) && (G+H+I!=15) && (A+D+G!=15) && (B+E+H!=15) && (C+F+I!=15) &&
      (A+E+I!=15) && (C+E+G!=15) && (A!=B!=C!=D!=E!=F!=G!=H!=I));
      cout<<A<<" "<<B<<" "<<C<<endl;
      cout<<D<<" "<<E<<" "<<F<<endl;
      cout<<G<<" "<<H<<" "<<I<<endl;
return 0;
}

When you write A!=B!=C!=D you should likely write A!=B && B!=C && C!=D , or perhaps !(A==B || B==C || C==D) .当你写A!=B!=C!=D你可能应该写A!=B && B!=C && C!=D ,或者!(A==B || B==C || C==D) The way you write it, the first comparison will result in an bool, which will then be compared to the next value.按照您的编写方式,第一次比较将产生一个布尔值,然后将其与下一个值进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM