zoj 1949 Error Correction
Error Correction
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
思路:判断行和列的数和是奇数的个数。
1.如果都是0.那么ok
2.如果都是1,那么可以修改一个,使得ok,用一个变量记录数和是奇数所在的行,用一个变量记录数和是奇数所在的列。
最后输出。
3.其他情况都是Corrupt。
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int n;
int i, j;
int NumOfRowOdd, NumOfColOdd, row_index, col_index, sum = ;
int m[][];
while(cin >> n){
if(n == )
break;
NumOfColOdd = ;
NumOfRowOdd = ;
for(i = ; i < n; i++){
for(j = ; j < n; j++){
cin >> m[i][j];
}
}
for(i = ; i < n; i++){
sum = ;
for(j = ; j < n; j++){
sum += m[i][j];
}
if(sum % == ){
NumOfRowOdd++;
row_index = i;
}
}
for(j = ; j < n; j++){
sum = ;
for(i = ; i < n; i++){
sum += m[i][j];
}
if(sum % == ){
NumOfColOdd++;
col_index = j;
}
}
if(NumOfColOdd > || NumOfRowOdd > ){
cout << "Corrupt" << endl;
continue;
}
if(NumOfColOdd == && NumOfRowOdd == ){
cout << "OK" << endl;
continue;
}
printf("Change bit (%d,%d)\n", row_index + , col_index + );
}
return ;
}
最新文章
- SharePoint 2016 必备组件离线安装介绍
- Android Intent Flags
- ELK——Logstash 2.2 mutate 插件【翻译+实践】
- php中each()与list()函数
- 【Leetcode】 - Divide Two Integers 位运算实现整数除法
- Orchard中的多语言功能
- Linux系统编程(32)—— socket编程之TCP服务器与客户端
- CodeForces 501B - Misha and Changing Handles
- 【angular】angular实现简单的tab切换
- Query DSL(1)
- hashlib,hmac,subprocess,configparser,xlrd,xlwt,xml模块基本功能
- 【Dubbo 源码解析】03_Dubbo Protocol&;Filter
- 谈谈 JavaScript 的正则表达式
- Python全栈学习_day004作业
- Bootstrap栅栏布局里col-xs-*、col-sm-*、col-md-*、col-lg-*之间的区别及使用方法
- Unity中的 原生插件/平台交互 原理
- 【转】移除HTML5 input在type=";number";时的上下小箭头
- 长期更新系列:C#知识点
- BZOJ 2330 糖果 差分约束求最小值
- talib 中文文档(七):Overlap Studies Functions