奇矩阵(2020强智杯)

题目链接:https://ac.nowcoder.com/acm/contest/9699/G

题目描述:

对于一个 n 行 m 列的矩阵 Ai,j,Bobo 定义第 i 行和第 j 行的距离
d(i,j)=k=1mAi,kAj,k∣, 其中
|x| 表示 x 的绝对值。如果对于所有的 1 ≤ i < j ≤ n,d(i, j) 都是奇数,Bobo称矩阵 Ai,j 是奇矩阵。 给出一个矩阵Ai,j ,判断它是否是奇矩阵。

输入描述:

输入文件包含多组数据,请处理到文件结束。
每组数据的第一行包含 2 个整数 n, m。接下来 n 行,其中第 i 行包含 m 个整数 Ai,1, ... ,Ai,m
 · 1 ≤ n, m ≤ 103
 · 0 ≤ Ai,j ≤ 10
 · n × m 的和不超过2×106

输出描述:

对于每组数据,如果它是奇矩阵,输出 Yes,否则输出 No。

示例:

输入:
1 2
0 0
2 3
1 2 3
2 1 4
3 3
1 2 3
4 5 6
7 8 9

输出:
Yes
Yes
No

题目分析:

尽量不要用vector,该定多大数组定多大数组。之前用vector错都不知道哪里错了,(明明是道水题)改成数组立马就对了!

代码:

C++:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 10e3+1;

int n, m;
ll a[mod][mod];
bool judge(int i, int j)
{
    ll ans = 0;
    ll tem;
    for (int z = 0;z < m;z++)
    {
        tem = abs(a[i][z] - a[j][z]);
        ans += tem;
    }
    if (ans % 2 == 0 && ans != 0)
        return false;
    return true;
}
int main()
{
    ios::sync_with_stdio(false);

    while (cin >> n >> m)
    {
        bool flag1 = true;
        for (int z = 0;z < n;z++)
            for (int x = 0;x < m;x++)
                cin>>a[z][x];

        for (int z = 0;z < n - 1 && flag1;z++)
            for (int x = z + 1;x <= n - 1 && flag1;x++)
                if (!judge(z, x))
                    flag1 = false;
        if (flag1)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
}

运行时间:108ms

使用内存:12348kb

神仙代码:

#include<bits/stdc++.h>
using namespace std;
inline int sca()
{
    int su=0,f=1;
    char c=getchar();
    while(!isdigit(c))
    {
        if(c=='-')
            f=-1;
        c=getchar();
    }
    while(isdigit(c))
        su=su*10+c-'0',c=getchar();
    return f*su;
}
int dis[1005];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int x,fl=0;
        for(int i=1; i<=n; ++i)
        {
            dis[i]=0;
            for(int j=1; j<=m; ++j)
                x=sca(),dis[i]+=x;
        }
        if(n==1)
        {
            puts("Yes");
            continue;
        }
        for(int i=1; i<=n; ++i){
            for(int j=i+1; j<=n; ++j)
                if(abs(dis[i]-dis[j])%2==0)
                {
                    fl=1;
                    break;
                }
                if(fl)break;}
        if(fl)
            puts("No");
        else
            puts("Yes");
    }
}

运行时间:29ms

占用内存:388kb

 
原文地址:https://www.cnblogs.com/zjw1324399/p/14279984.html