zoj 2835 Magic Square(set)
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)的更多相关文章
- codeforces 711B B. Chris and Magic Square(水题)
题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...
- ZOJ 2477 Magic Cube(魔方)
ZOJ 2477 Magic Cube(魔方) Time Limit: 2 Seconds Memory Limit: 65536 KB This is a very popular gam ...
- Xtreme8.0 - Magic Square 水题
Xtreme8.0 - Magic Square 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/ ...
- 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 ...
- 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 ...
- Little Elephant and Magic Square
Little Elephant loves magic squares very much. A magic square is a 3 × 3 table, each cell contains s ...
- B. Chris and Magic Square
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 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 ...
- 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 ...
随机推荐
- Java 修改编码格式的几种方式
1.工作空间 workspase Window→Preferences→General→Workspace→Text file encoding→other→UTF-8 2.项目编码格式 右键项目名→ ...
- pyinstaller打包遇到的错误处理
在python环境中运行中没问题,但打包时遇到以下问题 1.有时打包出现 UnicodeDecodeError错误, 可以改变cmd的编码(暂且这么叫),在cmd 中输入 chcp 65001,再次打 ...
- (026)[工具软件]剪切板管理:Ditto
剪切板管理软件:Ditto官网:http://ditto-cp.sourceforge.net/
- CentOS 6.9:MySQL Daemon failed to start.
[root@Server_1 12:02:58 ~ 25]#service mysqld start MySQL Daemon failed to start.Starting mysqld: [ro ...
- Castle.net
using System; using System.Collections.Generic; using System.Linq;using System.Text;using Castle.Act ...
- AJPFX实例集合嵌套之ArrayList嵌套ArrayList
案例:import java.util.ArrayList;import java.util.Iterator;import com.heima.bean.Person;public class De ...
- 第一次提交代码到github时经常遇到的问题
最近两年在OpenStack方面做了一些工作,写了一些实验性的plugin. 本着Open Source的共享精神,想尝试提交到github,以便他人能下载使用. 当你注册完github帐号之后,点击 ...
- c# sqlserver连接字符串
odbc: string cnnstring = @"Driver={SQL Server Native Client 11.0};Initial Catalog = sxquadb;ser ...
- win7创建无线(WIFI)cmd命令
1.创建无限热点:netsh wlan set hostednetwork mode=allow ssid=name key=12345678. 2.承载网络:netsh wlan start(关闭s ...
- 51nod 1417 天堂里的游戏
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 多年后,每当Noder看到吉普赛人,就会想起那个遥远的下午. Noder躺在草地上漫无目的的张望,二 ...