题目描述
Parity is an important concept in data transmission. Because the process is not error proof, parity is used to provide a check on whether or not data has been corrupted in transmission.
If even parity is being used, each byte will have an even number of 1 characters. If odd parity is being used, each byte will have an odd number of 1 characters.
In this problem, we are looking for a single bit that has been corrupted. To help you find it, the last byte is not part of the data being transmitted, but is a parity byte. Each bit of the parity byte will be used to make the corresponding bits in the data bytes odd or even depending on the parity being used.
If even parity is being used, each byte will have an even number of 1 characters. If odd parity is being used, each byte will have an odd number of 1 characters.
In this problem, we are looking for a single bit that has been corrupted. To help you find it, the last byte is not part of the data being transmitted, but is a parity byte. Each bit of the parity byte will be used to make the corresponding bits in the data bytes odd or even depending on the parity being used.
输入
The first line of input is a single integer, N (3 <= N <= 10), the number of bytes of data to follow. The next N lines each contain a single byte of data consisting of 8 characters separated by spaces. Each character will be either 1 or 0.
There will be one further line of 8 characters (again 1 or 0) which will be the parity byte.In the parity byte, each bit is a parity bit for the corresponding bits in the preceding N lines, using the same parity as is used by the data bytes. The parity byte itself may not show the same parity as the data bytes.
There will be one further line of 8 characters (again 1 or 0) which will be the parity byte.In the parity byte, each bit is a parity bit for the corresponding bits in the preceding N lines, using the same parity as is used by the data bytes. The parity byte itself may not show the same parity as the data bytes.
输出
Output 3 lines of information about the data in the input.
Line 1: Either the word Even or the word Odd to describe the parity being used by the bytes which are not broken.
Line 2: Byte <number> is broken
Line 3: Bit <number> is broken
<number> is the number of the appropriate byte or bit, where the first of each is number 1.
Line 1: Either the word Even or the word Odd to describe the parity being used by the bytes which are not broken.
Line 2: Byte <number> is broken
Line 3: Bit <number> is broken
<number> is the number of the appropriate byte or bit, where the first of each is number 1.
样例输入
3
1 0 1 0 1 1 1 0
1 1 0 1 1 1 0 0
1 0 1 1 1 0 0 0
0 0 1 1 1 1 0 1
样例输出
Odd
Byte 3 is broken
Bit 5 is broken
提示
Bytes 1 and 2 have an odd number of 1s but byte 3 has an even number. So odd parity is being used but byte 3 is broken.
The parity byte gives all columns of bits an odd number of 1s except for 5 where they are even, so bit 5 is broken. Bit 5 of byte 3 is corrupt.
【题解】:
很难得遇到一道组成原理为背景的题目了,深感欣慰,觉得这个题目有必要记录一下。
首先判断:奇数还是偶数判别法
其次判断那一个字节出现了问题。
最后判别哪一位上出现问题。
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 10; 4 int a[N][N]; 5 int s[N],n; 6 int row[N],col[N]; 7 int main() 8 { 9 scanf("%d",&n); 10 for(int i=1;i<=n;i++) for(int j=1;j<=8;j++) 11 scanf("%d",&a[i][j]),row[i]+=a[i][j],col[j] += a[i][j] ; 12 13 for(int i=1;i<=8;i++) 14 scanf("%d",&s[i]),col[i]+=s[i]; 15 16 int OE = 0 , Odd = 0 ,Even = 0; 17 for(int i=1;i<=n;i++){ 18 if( row[i]&1 ) Odd ++ ; 19 else Even ++ ; 20 } 21 if( Odd == n-1 ) OE = 1 ; 22 if( Even == n-1 ) OE = 0 ; 23 //Ans 1 24 if( OE&1 ){ 25 puts("Odd"); 26 }else{ 27 puts("Even"); 28 } 29 30 //Ans 2 31 for(int i=1;i<=n;i++){ 32 if( (OE & 1) && (row[i]%2==0) ){ 33 printf("Byte %d is broken ",i);break; 34 }else if( (OE%2==0) && (row[i]&1) ){ 35 printf("Byte %d is broken ",i);break; 36 } 37 } 38 39 //Ans 3 40 for(int j=1;j<=8;j++){ 41 if( (OE & 1) && (col[j]%2==0) ){ 42 printf("Bit %d is broken ",j);break; 43 }else if( (OE%2==0) && (col[j]&1) ){ 44 printf("Bit %d is broken ",j);break; 45 } 46 } 47 48 return 0; 49 }