Magic Square


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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
分析:根据幻方矩阵,可以计算出行和(列和,对角线和)为总和/行数;
 #include <iostream>
#include <cstdio>
#include <set>
using namespace std;
int m[][];
int main(){
int n, i, j;
int row_sum, col_sum;//行和,列和
int main_diagonal_sum, counter_diagonal_sum;//主对角线元素和,副对角线元素和
int sum;
set<int> st;
while(cin >> n){
if(n == )
break;
st.clear();
main_diagonal_sum = , counter_diagonal_sum = , sum = ;
for(i = ; i < n; i++){
for(j = ; j < n; j++){
cin >> m[i][j];
sum += m[i][j];
st.insert(m[i][j]);
}
}
if(st.size() != n * n){//很重要,矩阵中的数有可能重复,有重数的矩阵直接输出"No"
cout << "No" << endl;
continue;
}
int aver = sum / n;
//cout << aver << "a" << endl;
for(i = ; i < n; i++){
row_sum = ;
col_sum = ;
for(j = ; j < n; j++){
row_sum += m[i][j];
col_sum += m[j][i];
}
if(row_sum != aver || col_sum != aver){
cout << "No" << endl;
goto RL;
}
}
for(i = ; i < n; i++){
main_diagonal_sum += m[i][i];
counter_diagonal_sum += m[i][n - - i];
}
if(main_diagonal_sum != aver || counter_diagonal_sum != aver){
cout << "No" << endl;
continue;
}
cout << "Yes" << endl;
RL:
continue;
}
return ;
}

还有一种方法是将所有的和放到一个set集合,最后判断集合大小是不是1,若为1,则yes,否则no

 #include <iostream>
#include <cstdio>
#include <set>
using namespace std;
int m[][];
int main(){
int n, i, j;
int row_sum, col_sum;//行和,列和
int main_diagonal_sum, counter_diagonal_sum;//主对角线元素和,副对角线元素和
set<int> st;
while(cin >> n){
if(n == )
break;
st.clear();
main_diagonal_sum = , counter_diagonal_sum = ;
for(i = ; i < n; i++){
for(j = ; j < n; j++){
cin >> m[i][j];
st.insert(m[i][j]);
}
}
if(st.size() != n * n){//很重要,矩阵中的数有可能重复,有重数的矩阵直接输出"No"
cout << "No" << endl;
continue;
}
st.clear();
for(i = ; i < n; i++){
row_sum = ;
col_sum = ;
for(j = ; j < n; j++){
row_sum += m[i][j];
col_sum += m[j][i];
}
st.insert(row_sum);
st.insert(col_sum);
}
for(i = ; i < n; i++){
main_diagonal_sum += m[i][i];
counter_diagonal_sum += m[i][n - - i];
}
st.insert(main_diagonal_sum);
st.insert(counter_diagonal_sum);
if(st.size() != )
cout << "No" << endl;
else
cout << "Yes" << endl;
}
return ;
}
 

zoj 2835 Magic Square(set)的更多相关文章

  1. codeforces 711B B. Chris and Magic Square(水题)

    题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...

  2. ZOJ 2477 Magic Cube(魔方)

    ZOJ 2477 Magic Cube(魔方) Time Limit: 2 Seconds      Memory Limit: 65536 KB This is a very popular gam ...

  3. Xtreme8.0 - Magic Square 水题

    Xtreme8.0 - Magic Square 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/ ...

  4. Codeforces Round #369 (Div. 2) B. Chris and Magic Square 水题

    B. Chris and Magic Square 题目连接: http://www.codeforces.com/contest/711/problem/B Description ZS the C ...

  5. Chris and Magic Square CodeForces - 711B

    ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o ...

  6. Little Elephant and Magic Square

    Little Elephant loves magic squares very much. A magic square is a 3 × 3 table, each cell contains s ...

  7. B. Chris and Magic Square

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. Codeforces Round #369 (Div. 2) B. Chris and Magic Square (暴力)

    Chris and Magic Square 题目链接: http://codeforces.com/contest/711/problem/B Description ZS the Coder an ...

  9. CodeForces-259B]Little Elephant and Magic Square

      Little Elephant loves magic squares very much. A magic square is a 3 × 3 table, each cell contains ...

随机推荐

  1. oracle中的用户详解 【转】

      oracle中的用户很多,也很令初学者费解.oracle中的帐户分为两类:一类是必需的帐户,一类是存储各种应用的帐户 用户名 密码 描述 ANONYMOUS ANONYMOUS 访问http的匿名 ...

  2. [未读]JavaScript高效图形编程

    去年买来就一直搁置,因为是js游戏相关,暂时还用不到.

  3. HDU 5808 Price List Strike Back bitset优化的背包。。水过去了

    http://acm.hdu.edu.cn/showproblem.php?pid=5808 用bitset<120>dp,表示dp[0] = true,表示0出现过,dp[100] = ...

  4. Nginx重写规则

    Nginx的重写规则,依赖于pcre库(perl compatible regular expression).所以在安装的时候一定要让nginx支持这个功能,以及安装pcre-devel,prce. ...

  5. jmeter配置mysql数据库步骤

    安装环境: Windows10系统 jmeter版本:3.0版本 java1.8版本 安装步骤: 1.下载连接mysql数据库jar包,地址:http://download.csdn.net/deta ...

  6. 代码审查的艺术:Dropbox 的故事

    Dropbox 的 iOS 应用中的每一行代码,都是开始于被添加到 Maniphest 中的一个 bug 或者功能任务,Maniphest 是我们的任务管理系统.当一位工程师在上面接受一个任务,那么在 ...

  7. vs2013转为vs2010项目

    1.首先用记事本之类的工具打开.sln文件 打开后会看到如下信息 Format Version 12.00 就是指VS2013 VisualStudioVersion = 12.0.21005.1 指 ...

  8. Redis java操作客服端——jedis

    1. Jedis 需要把jedis依赖的jar包添加到工程中.Maven工程中需要把jedis的坐标添加到依赖. 推荐添加到服务层.happygo-content-Service工程中. 1.1. 连 ...

  9. Xilinx HLS

    Xilinx 的高层次综合(High Level Synthesis, HLS)技术是将C/C++/SystemC软件语言转换成Verilog或VHDL硬件描述语言的技术.现已应用在SDAccel,S ...

  10. IP地址 子网掩码 默认网关和DNS服务器的关系

    在过去,男人是需要能够上房揭瓦的,是要能够修水管的.现在的男人是需要会装系统的,会设置路由器的.世界变化太快! 废话不多说,本文来讨论一下电脑上最为常见的几个网络参数:IP地址.子网掩码.默认网关和D ...