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. 并查集 HDOJ 1232 畅通工程

    题目传送门 /* 并查集(Union-Find)裸题 并查集三个函数:初始化Init,寻找根节点Find,连通Union 考察:连通边数问题 */ #include <cstdio> #i ...

  2. 1-13Object类之toString方法

    Object中的toString方法 SUN在Object类中设计toString方法的目的:返回java对象的字符串表示形式. 在现实的开发过程中,Object中的toString方法就是要被重写的 ...

  3. Unity基础知识

    hierarchy视图选中,点击scene视图,按f键聚焦 persp相当于是透视视野 在persp模式下,物体在scene界面上所呈现的画面是给人一种距离摄像头近的物体显示的大,距离摄像头远的物体显 ...

  4. Windows下Apache应用环境塔建安全设置(目录权限设置)

    目的:为Apache,php配置受限制的用户权限.保护系统安全.需要的朋友可以参考下. 环境配置情况: apache安装目录:d:\www-s\apache php目录:d:\www-s\php5 m ...

  5. RabbitMQ八:交换机类型Exchange Types--Topic介绍

    前言 上一章节,我们说了两个类型,本章我们说一下其三:Topic Exchange Topic Exchange  Topic Exchange – 将路由键和某模式进行匹配.此时队列需要绑定要一个模 ...

  6. Asp.net MVC + Vue.js

    @{ Layout = null; } <!DOCTYPE html><html> <head> <meta charset="UTF-8" ...

  7. SSM-WebMVC(三)

    SSM-WebMVC(三) 一.Annotated Controllers ​ 应用程序控制器 handlerMethod(处理方法) ㈠方法入参 ​ (springmvc针对于在controller ...

  8. hihocoder offer收割编程练习赛8 C 数组分拆

    思路:(引自bfsoyc的回答:http://hihocoder.com/discuss/question/4160) 动态规划.状态dp[i]表示 前i个数的合法的方案数,转移是 dp[i] = s ...

  9. Arduino中数据类型转换 float/double转换为char 亲测好使,dtostrf()函数

    如何轻松玩转Arduino单片机,当我在进行数据转换的时候,遇到了问题,尝试了C语言和C++中的好多函数,都没有达到将float型数据转换为char型的目的.苦苦查阅资料后,终于找到了一个大神级函数! ...

  10. element ui select组件和table做分页完整功能和二级联动效果

    <template> <div class="index_box"> <div class="search_box"> &l ...