zoukankan      html  css  js  c++  java
  • Codeforces_711_B

    http://codeforces.com/problemset/problem/711/B

    比较简单,过程有点繁琐,先找一行包含那个0的行,得到和,以此填出0位置的值,然后判断这个矩阵是否符合条件。

    要注意的是,n=1的情况,数据超了int,结果不为负。

    #include<iostream>
    #include<cstdio>
    using namespace std;
    long long a[505][505];
    int main()
    {
        int n,x,y;
        cin >> n;
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= n;j++)
            {
                cin >> a[i][j];
                if(!a[i][j])
                {
                    x = i;
                    y = j;
                }
            }
        }
        if(n == 1)
        {
            printf("1
    ");
            return 0;
        }
        long long sum = 0;
        for(int i = 1;i <= n;i++)
        {
            if(i != x)
            {
                for(int j = 1;j <= n;j++)
                {
                    sum += a[i][j];
                }
                break;
            }
        }
        long long now = 0;
        for(int i = 1;i <= n;i++)
        {
            now += a[x][i];
        }
        a[x][y] = sum-now;
        for(int i = 1;i <= n;i++)
        {
            long long temp1 = 0,temp2 = 0;
            for(int j = 1;j <= n;j++)
            {
                temp1 += a[i][j];
                temp2 += a[j][i];
            }
            if(temp1 != sum || temp2 != sum)
            {
                cout << -1 << endl;
                return 0;
            }
        }
        long long temp1 = 0,temp2 = 0;
        for(int i = 1;i <=n;i++)
        {
            temp1 += a[i][i];
            temp2 += a[i][n-i+1];
        }
        if(temp1 != sum || temp2 != sum)
        {
            cout << -1 << endl;
            return 0;
        }
        if(a[x][y] <= 0)    cout << -1 << endl;
        else                cout << a[x][y] << endl;
        return 0;
    }
  • 相关阅读:
    python 类
    hdu 5761 Rowe Bo 微分方程
    calendar 示例
    hdu 5753 Permutation Bo
    hdu 5752 Sqrt Bo
    finally 语句
    throws 和 throw
    python打开.pkl的文件并显示里面的内容
    Python 类变量,成员变量,静态变量,局部变量
    python 实例方法,类方法,静态方法,普通函数
  • 原文地址:https://www.cnblogs.com/zhurb/p/5858575.html
Copyright © 2011-2022 走看看