下面是这道题的C++ 代码:
#include <iostream> #include <algorithm> using namespace std;
int main() { int num[9] = {1,2,3,4,5,6,7,8,9};
sort(num, num+9); do { int sum1 = num[0] + num[1] + num[2]; int sum2 = num[3] + num[4] + num[5]; if(sum1 == sum2) { int sum3 = num[6] + num[7] + num[8]; if(sum2 == sum3) { int sum4 = num[0] + num[3] + num[6]; if(sum3 == sum4) { int sum5 = num[1] + num[4] + num[7]; if(sum4 == sum5) { int sum6 = num[2] + num[5] + num[8]; if(sum5 == sum6) { int sum7 = num[2] + num[4] + num[6]; if(sum6 == sum7) { int sum8 = num[0] + num[4] + num[8]; if(sum7 == sum8) { cout<<num[0]<<num[1]<<num[2]<<endl; cout<<num[3]<<num[4]<<num[5]<<endl; cout<<num[6]<<num[7]<<num[8]<<endl; cout<<endl; } } } } } } } }while (next_permutation(num, num+9));
return 0; }