Problem Description
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
题目大意:
输入一个数字,如果每一行每一列加起来都是偶数那就输出OK,如果改一个数字就可以满足上述条件,那就输出Change bit (第几行,第几列),如果改一个数字都不能满足的话就输出Corrupt。
代码:
#include <stdio.h>
#include<iostream>
#include <stdlib.h>
#include<algorithm>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n==0)
break;
int arr[n+5][n+5],hang=0,lie=0,hangou=0,lieou=0,flag=0,a=0,b=0;
for(int i=1;i<=n;i++)
{
if(hangou%2!=0)
{
a=i-1;
hang++;
hangou=0;
}
if(hang==2){
flag=1;
}
for(int j=1;j<=n;j++)
{
scanf("%d",&arr[i][j]);
if(arr[i][j]==1)
hangou++;
}
}
if(flag==1)
{
printf("Corrupt\n");
continue;
}
// printf("%d",hang);
for(int i=1;i<=n;i++)
{
if(lieou%2!=0)
{
b=i-1;
lie++;
lieou=0;
}
if(lie==2){
flag=1;
break;}
for(int j=1;j<=n;j++)
{
if(arr[j][i]==1)
lieou++;
}
}
if(flag==1||(a!=0&&b==0)||(a==0&&b!=0))
printf("Corrupt\n");
else if(a==0&&b==0)
printf("OK\n");
else
printf("Change bit (%d,%d)\n",a,b);
}
return 0;
}