Restore 数学题,水题(转)
来源:互联网 发布:清风dj软件下载 编辑:程序博客网 时间:2024/05/01 21:41
C - Restore
Time Limit:25000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
Gym 100735E
Description
standard input/output
Statements
Given a matrix A of size N * N. The rows are numbered from 0 to N-1, the columns are numbered from 0 to N-1. In this matrix, the sums of each row, the sums of each column, and the sum of the two diagonals are equal.
For example, a matrix with N = 3:
The sums of each row:
2 + 9 + 4 = 15
7 + 5 + 3 = 15
6 + 1 + 8 = 15
The sums of each column:
2 + 7 + 6 = 15
9 + 5 + 1 = 15
4 + 3 + 8 = 15
The sums of each diagonal:
2 + 5 + 8 = 15
4 + 5 + 6 = 15
As you can notice, all sums are equal to 15.
However, all the numbers in the main diagonal (the main diagonal consists of cells (i, i)) have been removed. Your task is to recover these cells.
Input
The first line contains the dimension of the matrix, n (1 ≤ n ≤ 100).
The following n lines contain n integers each, with removed numbers denoted by 0, and all other numbers - 1012 ≤ Aij ≤ 1012.
Output
The restored matrix should be outputted in a similar format, with n lines consisting of n integers each.
Sample Input
Input
3
0 9 4
7 0 3
6 1 0
Output
2 9 4
7 5 3
6 1 8
这题就属于数学题了。。。有一个式子很容易想到,但是怎样把这个式子转化成我们需要的式子用了我好长时间。
下面用题目例子说明
0 9 4
7 0 3
6 1 0
sum1=0+9+4=13
sum1=7+0+3=10
sum1=6+1+0=7
a[1][1]=x1, a[2][2]=x2, a[3][3]=x3,
所以有sum1+x1==sum2+x2==sum3+x3==x1+x2+x3
其实这个式子很容易得到,下面就是放大招的时候了
sum1+x1+sum2+x2+sum3+x3==3(x1+x2+x3)
->sum1+sum2+sum3==2(x1+x2+x3)
->x1+x2+x3==(sum1+sum2+sum3)/2;
呃呃,这就能求出x1,x2,x3了。
#include <cstdio>using namespace std;long long a[110][110];long long row[110];int n;int main(){ while(scanf("%d",&n)!=EOF) { long long sum=0; int i, j; for(i=0; i<n; i++) { for(j=0; j<n; j++) { scanf("%I64d",&a[i][j]); row[i]+=a[i][j]; } } for(i=0; i<n; i++) sum+=row[i]; sum/=(n-1); for(i=0; i<n; i++) a[i][i]=sum-row[i]; for(i=0; i<n; i++) { for(j=0; j<n-1; j++) printf("%I64d ",a[i][j]); printf("%I64d\n",a[i][n-1]); } }}
转自同学博客:http://blog.csdn.net/u011613367/article/details/51040207
- Restore 数学题,水题(转)
- HDU 2178 猜数字(水题,数学题)
- hihoCoder 1234 Fractal(精度+数学题+水题)
- (转)经典搞笑奥林劈叉数学题
- (转)挖矿的数学题
- POJ1844 Sum【水题+数学题】
- NUC1196 Sum【水题+数学题】
- NUC1312 Sum【水题+数学题】
- ACM数学题--转
- poj1152 数学题(进制位)
- hdu 2178 (数学题)
- poj 1019(数学题)
- 糖果传递 (数学题)
- cf(cards)数学题
- hdu 2832(数学题)
- 数学题(1)
- HDU5100 Chessboard(数学题)
- nyoj94cigarettes(数学题)
- DQS的模板复习计划
- 文章标题
- win2008 iis ftp 530 User cannot log in
- 栈和队列(思想+伪代码+部分代码)
- Java String.split()用法小结
- Restore 数学题,水题(转)
- HDU 5294 Tricks Device 最短路建图+最小割(最大流)
- 基于数学形态学的图像处理
- Android中常用的Linux指令以及Root原理浅析
- 02_编程实现1!+2!+……+10!的和
- 码农小汪-JavaEE乱码 response.setCharacterEncoding 入URIEncoding="utf-8" response.setContentType
- 在线绘图
- Swift中的宏定义
- 数字在排序数组中出现的次数