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 ...
随机推荐
- ACM_求第k大元素(两次二分)
求第k大 Time Limit: 6000/3000ms (Java/Others) Problem Description: 给定两个数组A和B,大小为N,M,每次从两个数组各取一个数相乘放入数组C ...
- 【转】Android官方架构项目之MVP + Clean
首先,不了解 Clean 架构的可以看看这个,不过也没关系,阅读本文后你也许会对Clean架构思想有一个认识. 对比MVP项目的结构图,我们发现不同之处是新增的这个Domain Layer这层,来隔离 ...
- SQL异常为"当IDENTITY_INSERT设置为OFF时" 的解决
误删数据库时,可以利用insert插入删除的数据,但是有时表可能有自增字段如id.这是插入数据如果包含自增字段就会出现错误,提示"IDENTITY_INSERT设置为OFF,插入失败&quo ...
- ios 从相册视频中获取视频截图
//给image添加个分类 +(UIImage *)getImage:(NSURL: *)videoURL { AVURLAsset *asset = [[AVURLAsset alloc] init ...
- ubuntu服务器切换语言
如果在安装Ubuntu Server时选择了中文,在系统安装完毕后,默认是中文,在操作时经常会显示乱码,如果需要设置回英文,则修改/etc/default/locale,将 LANG="cn ...
- 如何实现Windows宿主系统和虚拟机ubuntu系统文件互相访问
我的宿主操作系统是Windows 10,使用Oracle的Virtual Box安装了Ubuntu. 因为工作需要我经常得在两个系统之间互相拷贝一些数据,下面是具体步骤,可以实现Windows 10和 ...
- PHP一句话后门过狗姿势万千之后门构造与隐藏
第二章节主要带给大家一些后门构造思路,与安全狗文件特征检测的机制. 另外强调一下,这篇文章需要大家对于php有一定的认识. 本章节分为三大部分,第一部分针对初级,分析菜刀php代码的执行过程,较基础: ...
- 在Oracle用SQL处理以 System.currentTimeMillis
有時為了系統的需求會紀錄到毫秒(Millisecond),我們會接將得到的值寫入db,但是如果要用SQL 做時間範圍的搜尋,有以下做法( systemdate欄位存放System.currentTim ...
- python程序的编辑和运行、变量
第一个python程序 python是解释型弱类型高级语言 常见的python解释器CPython.IPython.pypy.JPython.IronPython 方法一.python程序可以写在命令 ...
- 关于websocket的代码,实现发送信息和监听信息(前端 后端(node.js))
文件结构 node.js代码 // 需要HTTP 模块来启动服务器和Socket.IOvar http= require('http');var fs = require('fs');// 在8080 ...