Restore 数学题,水题(转)

news/2024/7/5 18:28:33

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


http://www.niftyadmin.cn/n/2246210.html

相关文章

unity实战 手机屏幕适配

使用背景 为了在UI中使用特效层&#xff0c;项目Canvas采用了Screen Space-Camera类型 UI的Scale Mode 选择的是Scale With Screen Size 的Expand&#xff0c;画布大小填的是1334, 750 在该选择下&#xff0c;会自动根据分辨率适配宽度/高度。 自动适配的规则是&#xff1a; 屏…

八皇后问题---递归回溯

每次需要满足的条件&#xff1a;abs(x[j]-x[k])abs(j-k) || x[j]x[k] #include <iostream> #include <cmath> using namespace std; const int num8; int sum0; int x[num]{0}; bool place (int k) {for(int j1;j<k;j){if(abs(x[j]-x[k])abs(j-k)||x[j]x[k])re…

容器监控实践—cAdvisor

概述 为了解决docker stats的问题(存储、展示)&#xff0c;谷歌开源的cadvisor诞生了&#xff0c;cadvisor不仅可以搜集一台机器上所有运行的容器信息&#xff0c;还提供基础查询界面和http接口&#xff0c;方便其他组件如Prometheus进行数据抓取&#xff0c;或者cadvisor inf…

染陌足迹——SeeConf2019

水文预警&#xff01;&#xff01;多图预警&#xff01;&#xff01; 听了一整天&#xff0c;每位讲师的演讲都十分精彩&#xff0c;每个主题都能给我从另一个角度的思考与启迪&#xff0c;大部分主题都是以未来的交互设计作为维度来展开思考与探讨&#xff0c;以故事或者事例的…

Java的四种线程池的使用,以及自定义线程工厂

四种线程池四种线程池分别是&#xff1a;newCachedThreadPool、newFixedThreadPool 、newScheduledThreadPool 和newSingleThreadExecutor &#xff0c;下面对这几个线程池一一讲解。newCachedThreadPool&#xff1a;可缓存的线程池源码&#xff1a;public static ExecutorServ…

30K iOS程序员的简述:如何快速进阶成为高级开发人员 ...

前言&#xff1a; 本篇文章适用于所有在这个行业已经有了几年时间后想要在职业生涯中取得突破的开发人员&#xff0c;编程人员和程序员&#xff08;或者你可能刚刚开始&#xff0c;但希望你能看到你的路径&#xff09; 本文适合那些有着简单愿望的人&#xff1a;你想成为一名高…

初涉Windows Mobile 5.0的开发(起步篇)[Teaks]

这个周末构建了Windows Mobile 5.0的开发环境&#xff0c;也简单测试了一下&#xff0c;算是起步吧&#xff0c;下面是简要步骤&#xff1a; 工欲善其事&#xff0c;必先利其器。首先安装工具 1. 安装Visual Studio 2005 2.开发工具包, 我下的是800多M的那个&#xff0c;Window…

用Kotlin写一个基于Spring Boot的RESTful服务

Spring太复杂了&#xff0c;配置这个东西简直就是浪费生命。尤其在没有什么并发压力&#xff0c;随便搞一个RESTful服务 让整个业务跑起来先的情况下&#xff0c;更是么有必要纠结在一堆的XML配置上。显然这么想的人是很多的&#xff0c;于是就 有了Spring Boot。又由于Java 8太…