zoukankan      html  css  js  c++  java
  • POJ 2260 Error Correction(水题)

    A boolean matrix has the parity property when each row and each column has an even sum, i.e. contains an even number of bits which are set. Here's a 4 x 4 matrix which has the parity property:
    1 0 1 0

    0 0 0 0

    1 1 1 1

    0 1 0 1

    The sums of the rows are 2, 0, 4 and 2. The sums of the columns are 2, 2, 2 and 2.
    Your job is to write a program that reads in a matrix and checks if it has the parity property. If not, your program should check if the parity property can be established by changing only one bit. If this is not possible either, the matrix should be classified as corrupt.

    Input
    The input will contain one or more test cases. The first line of each test case contains one integer n (n<100), representing the size of the matrix. On the next n lines, there will be n integers per line. No other integers than 0 and 1 will occur in the matrix. Input will be terminated by a value of 0 for n.
    Output
    For each matrix in the input file, print one line. If the matrix already has the parity property, print "OK". If the parity property can be established by changing one bit, print "Change bit (i,j)" where i is the row and j the column of the bit to be changed. Otherwise, print "Corrupt".
    Sample Input
    4
    1 0 1 0
    0 0 0 0
    1 1 1 1
    0 1 0 1
    4
    1 0 1 0
    0 0 1 0
    1 1 1 1
    0 1 0 1
    4
    1 0 1 0
    0 1 1 0
    1 1 1 1
    0 1 0 1
    0
    Sample Output
    OK
    Change bit (2,3)
    Corrupt

    题意:

    给出一个只由0和1组成的n*n矩阵,如果它的每一行和每一列之和均是偶数,则我们称这个矩阵是“沃兹基扁德矩阵”,现在给你一个矩阵,请你判断它是否为“沃兹基扁德矩阵”,若不是,请判断是否存在一种方法,改变矩阵中的一个数字(0变1,1变0)使其变为“沃兹基扁德矩阵”。

    题解:

    水题,直接模拟就好了。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    int a[105][105];
    int row[105],col[105];
    int n;
    bool check()
    {
        for(int i=0;i<n;i++)
            if(row[i]%2||col[i]%2)
                return false;
        return true;
    }
    int main()
    {
        while(cin>>n&&n)
        {
            memset(row,0,sizeof(row));
            memset(col,0,sizeof(col));    
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                {
                    cin>>a[i][j];
                    row[i]+=a[i][j];
                    col[j]+=a[i][j];
                }
            if(check())
            {
                cout<<"OK"<<endl;
            }
            else
            {
                int ansi,ansj;
                bool flag=false;
                for(int i=0;i<n;i++)
                {
                    if(row[i]%2)
                    {
                        for(int j=0;j<n;j++)
                        {
                            if(col[j]%2)
                            {
                                ansi=i,ansj=j;
                                row[i]--,col[j]--;
                                flag=true;
                                break;
                            }
                        }
                        if(flag)
                            break;
                    }
                }
                if(flag&&check())
                    printf("Change bit (%d,%d)
    ",ansi+1,ansj+1);
                else
                    cout<<"Corrupt"<<endl;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Django Admin 日期字段格式化(转载)
    关于跨域请求和django处理跨域请求最佳解决方案的总结(转载)
    19.循环语句例题
    18.条件分支例题-道理:开发有两个要求:1能实现功能 2.代码最优
    17.循环语句-while循环 do while循环
    16.循环语句---for循环 for break 、for continue 、 for continue配合label写法
    15.条件?value1 : value2 三目运算符-判断语句
    14.switch case break 判断语句
    13.if 条件判断语句if 、if else
    12.经典计算题
  • 原文地址:https://www.cnblogs.com/orion7/p/7930660.html
Copyright © 2011-2022 走看看