zoj 2835 Magic Square(set)
In recreational mathematics, a magic square of n-degree is an arrangement of n2 numbers, distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant. For example, the picture below shows a 3-degree magic square using the integers of 1 to 9.
Given a finished number square, we need you to judge whether it is a magic square.
Input
The input contains multiple test cases.
The first line of each case stands an only integer N (0 < N < 10), indicating the degree of the number square and then N lines follows, with N positive integers in each line to describe the number square. All the numbers in the input do not exceed 1000.
A case with N = 0 denotes the end of input, which should not be processed.
Output
For each test case, print "Yes" if it's a magic square in a single line, otherwise print "No".
Sample Input
2
1 2
3 4
2
4 4
4 4
3
8 1 6
3 5 7
4 9 2
4
16 9 6 3
5 4 15 10
11 14 1 8
2 7 12 13
0
Sample Output
No
No
Yes
Yes
分析:根据幻方矩阵,可以计算出行和(列和,对角线和)为总和/行数;
1 #include <iostream> 2 #include <cstdio> 3 #include <set> 4 using namespace std; 5 int m[10][10]; 6 int main(){ 7 int n, i, j; 8 int row_sum, col_sum;//行和,列和 9 int main_diagonal_sum, counter_diagonal_sum;//主对角线元素和,副对角线元素和 10 int sum; 11 set<int> st; 12 while(cin >> n){ 13 if(n == 0) 14 break; 15 st.clear(); 16 main_diagonal_sum = 0, counter_diagonal_sum = 0, sum = 0; 17 for(i = 0; i < n; i++){ 18 for(j = 0; j < n; j++){ 19 cin >> m[i][j]; 20 sum += m[i][j]; 21 st.insert(m[i][j]); 22 } 23 } 24 if(st.size() != n * n){//很重要,矩阵中的数有可能重复,有重数的矩阵直接输出"No" 25 cout << "No" << endl; 26 continue; 27 } 28 int aver = sum / n; 29 //cout << aver << "a" << endl; 30 for(i = 0; i < n; i++){ 31 row_sum = 0; 32 col_sum = 0; 33 for(j = 0; j < n; j++){ 34 row_sum += m[i][j]; 35 col_sum += m[j][i]; 36 } 37 if(row_sum != aver || col_sum != aver){ 38 cout << "No" << endl; 39 goto RL; 40 } 41 } 42 for(i = 0; i < n; i++){ 43 main_diagonal_sum += m[i][i]; 44 counter_diagonal_sum += m[i][n - 1 - i]; 45 } 46 if(main_diagonal_sum != aver || counter_diagonal_sum != aver){ 47 cout << "No" << endl; 48 continue; 49 } 50 cout << "Yes" << endl; 51 RL: 52 continue; 53 } 54 return 0; 55 }
还有一种方法是将所有的和放到一个set集合,最后判断集合大小是不是1,若为1,则yes,否则no
1 #include <iostream> 2 #include <cstdio> 3 #include <set> 4 using namespace std; 5 int m[10][10]; 6 int main(){ 7 int n, i, j; 8 int row_sum, col_sum;//行和,列和 9 int main_diagonal_sum, counter_diagonal_sum;//主对角线元素和,副对角线元素和 10 set<int> st; 11 while(cin >> n){ 12 if(n == 0) 13 break; 14 st.clear(); 15 main_diagonal_sum = 0, counter_diagonal_sum = 0; 16 for(i = 0; i < n; i++){ 17 for(j = 0; j < n; j++){ 18 cin >> m[i][j]; 19 st.insert(m[i][j]); 20 } 21 } 22 if(st.size() != n * n){//很重要,矩阵中的数有可能重复,有重数的矩阵直接输出"No" 23 cout << "No" << endl; 24 continue; 25 } 26 st.clear(); 27 for(i = 0; i < n; i++){ 28 row_sum = 0; 29 col_sum = 0; 30 for(j = 0; j < n; j++){ 31 row_sum += m[i][j]; 32 col_sum += m[j][i]; 33 } 34 st.insert(row_sum); 35 st.insert(col_sum); 36 } 37 for(i = 0; i < n; i++){ 38 main_diagonal_sum += m[i][i]; 39 counter_diagonal_sum += m[i][n - 1 - i]; 40 } 41 st.insert(main_diagonal_sum); 42 st.insert(counter_diagonal_sum); 43 if(st.size() != 1) 44 cout << "No" << endl; 45 else 46 cout << "Yes" << endl; 47 } 48 return 0; 49 }