Little Elephant and Magic Square

Little Elephant loves magic squares very much.

A magic square is a 3?×?3 table, each cell contains some positive integer. At that the sums of integers in all rows, columns and diagonals of the table are equal. The figure below shows the magic square, the sum of integers in all its rows, columns and diagonals equals 15.

The Little Elephant remembered one magic square. He started writing this square on a piece of paper, but as he wrote, he forgot all three elements of the main diagonal of the magic square. Fortunately, the Little Elephant clearly remembered that all elements of the magic square did not exceed 105.

Help the Little Elephant, restore the original magic square, given the Elephant‘s notes.

Input

The first three lines of the input contain the Little Elephant‘s notes. The first line contains elements of the first row of the magic square. The second line contains the elements of the second row, the third line is for the third row. The main diagonal elements that have been forgotten by the Elephant are represented by zeroes.

It is guaranteed that the notes contain exactly three zeroes and they are all located on the main diagonal. It is guaranteed that all positive numbers in the table do not exceed 105.

Output

Print three lines, in each line print three integers — the Little Elephant‘s magic square. If there are multiple magic squares, you are allowed to print any of them. Note that all numbers you print must be positive and not exceed 105.

It is guaranteed that there exists at least one magic square that meets the conditions.

Example

Input

0 1 11 0 11 1 0

Output

1 1 11 1 11 1 1

Input

0 3 65 0 54 7 0

Output

6 3 65 5 54 7 4

本题为数学题,列方程可得解将第一个至第九个元素分别用字母代表,即方阵为 a b c                       d e f                       g h ia,e,h为未知数

代码如下
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7  
 8 int main()
 9 {
10     int A,B,C,D,E,F,G,H,I;
11     scanf("%d%d%d%d%d%d%d%d%d",&A,&B,&C,&D,&E,&F,&G,&H,&I);
12     I = D + (F - H)/2;
13     A = (F+H)/2;
14     E = B+C - I;
15     printf("%d %d %d\n%d %d %d\n%d %d %d\n",A,B,C,D,E,F,G,H,I);
16     return 0;
17 }


原文地址:https://www.cnblogs.com/ScaleCX/p/8322442.html

时间: 2024-10-17 22:47:28

Little Elephant and Magic Square的相关文章

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 some positive integer. At that the sums of integers in all rows, columns and diagonals of the table are equal. The figure below shows the magic square,

IOI&#39;96 Magic Square解题报告

IOI‘96 Magic Square(魔版) 题目由JerryXie翻译. Magic Square(msqaure.cpp/c/pas) [问题描述] 在魔方的成功之后,卢比克先生发明了它的二维版本,叫做魔版.它是一个由八个方块组成的表格. 在本题中我们认为每一个方块都有不同的颜色,这些颜色将会用1~8共8个整数表示.一个表格的状态将会以一个由颜色组成的序列表示,顺序是从表格左上角沿顺时针方向给出.比如上面这个表格,序列为(1,2,3,4,5,6,7,8),这被称作初始状态. 魔版有三种基本

Sicily 1302 Magic Square (数论)

链接:http://soj.me/1302 题目中有图和表格,所以就不把题目信息列出来了,打开链接直接看就行了... 分析: 从填幻方的过程,可以判断一下几点: 1>.幻方的每一列都是向下发展的: 2>.填入的前n个数都刚好是每一列第一个填入的数: 从这两点可以推出下面几点: (1). 每一列的发展是平面的(即每一列的个数是一样的,特别的,只有一列少一): (2). 右下角那个数肯定是填入的最大的: (3). 当我们填入到右下角的时候,每一列的个数都刚好是n/2(注意n是奇数,这里的除是整除)

Magic Square

这道题是比较简单的,三个字--找规律.思路也应该是比较清晰的,下面,我们先看一下题目吧. Description 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

codeforces 711B - Chris and Magic Square

题目链接:http://codeforces.com/problemset/problem/711/B 题目大意: 输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 使得矩阵 行列斜 和全部相等. 代码状态: 一把辛酸泪

CF 711B - Chris and Magic Square

挺简单的一道题,但是做的时候没想好就开始写代码了,导致迷之WA,还是要多练习啊. #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <set> #define LL long long int using namespace std; LL Map[505][505]; int main() { cin.sync_with_st

CodeForces 711B Chris and Magic Square (暴力,水题)

题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等. 析:首先n为1,就随便填,然后就是除了0这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他的算出来,然后再作差就算这个数了, 然后再去验证其他的对不对就好了.除了n为1,其他的都是唯一解应该.或者没有. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #i

XMU1016 Magic Square【幻方构造】

题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1016 题目大意: 给你一个整数N,然后输出一个N阶幻方. 解题思路: 关于幻方.搜索肯定超时. 可是我们能够直接构造一个幻方. 就是依据奇数阶(2*M+1)幻方. 单偶数阶(4*M+2)幻方.双偶数阶(4*M)幻方等不同的构造方法来构造. 參考博客:http://blog.csdn.net/u012317281/article/details/38051185 1 奇数阶 (2*M+

codeforces #369div2 B. Chris and Magic Square

题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 分析:题目不难,找到要填的那个数填进去,然后循环比较每行每列以及对角线的和是否相等,题目提交上去卡了几次要注意几点 注意:1.答案数据范围1<=x<=1e18,要用 long long 2.特殊情况,n==1时,由于一定有要填的数,所以一定有解 3.反正就是要注意看清楚题目和数据边界情况处理啦 1 #include<iostream> 2 #include<cstdio> 3 #includ