Restore 数学题,水题(转)
2016-04-06 21:24
357 查看
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了。
转自同学博客:/article/7980672.html
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]); } } }
转自同学博客:/article/7980672.html
相关文章推荐
- 基于数学形态学的图像处理
- HDU 5294 Tricks Device 最短路建图+最小割(最大流)
- 码农小汪-JavaEE乱码 response.setCharacterEncoding 入URIEncoding="utf-8" response.setContentType
- 字符串变量String的常用操作
- Android开发之非Activity类型的Context启动一个目标Activity
- 《剑指Offer》学习笔记——数值的整数次方
- 02_编程实现1!+2!+……+10!的和
- Swift中的宏定义
- cocos2dx-3.10学习之HelloWorld解析
- Android中常用的Linux指令以及Root原理浅析
- 【BZOJ-1500】维修数列 Splay
- 广商14级软件工程分数:第四回合
- Maven 包命令
- 复利计算--结对
- Zabbix笔记
- win2008 iis ftp 530 User cannot log in
- Scala中Iterator迭代器一个魔幻的地方
- DQS的模板复习计划
- poj1017(贪心)
- Java String.split()用法小结