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 ...
随机推荐
- JSP分页技术的实现(利用当前页进行前后加减,并利用href进行当前页面传值,传值当然是那个当前值变量)
一.可滚动结果集 Connection con = DriverManager.getConnection(); PreparedStatement stmt = con.prepareStat ...
- IOS-关闭(退)键盘事件--转
方法: 1.手势(触背景)关闭键盘 -(void)tapBackground //在ViewDidLoad中调用{ UITapGestureRecognizer * tap = [[UITapG ...
- python对ini配置文件处理
实例文件: [root@docker2 ~]# cat test.ini [base] host = 192.168.88.121 port = 3306 user = root path = /ho ...
- Vue 页面加载闪现代码问题
CSS中 [v-cloak] { display: none; } HTML中 <div v-cloak> {{ message }} </div> 显示代码主要是{{}}这个 ...
- 【Laravel】 常用命令
自动创建项目 laravel new || laravel new xxx || composer create-project --prefer-dist laravel/laravel blog ...
- iOS面试题之runloop
本文围绕以下几个部分展开对runloop的叙述. 1.runloop是什么/runloop的概念? 2.NSRunLoop 和 CFRunLoopRef? 3.runloop和线程的关系? 4.run ...
- 迭代器模式及php实现
迭代器模式: 迭代器模式是遍历集合的成熟模式,迭代器模式的关键是将遍历集合的任务交给一个叫做迭代器的对象,它的工作时遍历并选择序列中的对象,而客户端程序员不必知道或关心该集合序列底层的结构. 角色: ...
- 【学习笔记】响应式布局的常用解决方案(媒体查询、百分比、rem、和vw/vh)
原文转载:https://blog.csdn.net/sinat_17775997/article/details/81020417 一.媒体查询 不同物理分辨率的设备,在还原设计稿时,css中设置的 ...
- Backbone.js之Todo源码浅析
相信每个接触了解过backbone的人都知道todo,网上的关于它的分析教程也都分析乱了.但是,知识只有自己学习领悟才是自己的,话不多说,正文开始. 在分析todo的源码之前,首先我们要知道todo具 ...
- Verilog设计中的锁存器
问题: 什么是锁存器? 什么时候出现锁存器? 锁存器对电路有什么影响? 如何在FPGA设计中避免锁存器? 在FPGA设计中应该避免锁存器.实际上,锁存器与D触发器实现的逻辑功能基本相同,都有暂存数据的 ...