HDU2700 Parity【水题】

来源:互联网 发布:java return递归 编辑:程序博客网 时间:2024/05/01 07:55
Parity

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2634    Accepted Submission(s): 2031

Problem Description
A bit string has odd parity if the number of 1's is odd. A bit string has even parity if the number of 1's is even.Zero is considered to be an even number, so a bit string with no 1's has even parity. Note that the number of
0's does not affect the parity of a bit string.
 
Input
The input consists of one or more strings, each on a line by itself, followed by a line containing only "#" that signals the end of the input. Each string contains 1–31 bits followed by either a lowercase letter 'e' or a lowercase letter 'o'.
 
Output
Each line of output must look just like the corresponding line of input, except that the letter at the end is replaced by the correct bit so that the entire bit string has even parity (if the letter was 'e') or odd parity (if the letter was 'o').
 
Sample Input
101e
010010o
1e
000e
110100101o
#
 
Sample Output
1010
0100101
11
0000
1101001010
 
Source

2008 Mid-Central USA


题目大意:给你一个01字符串,末尾为'e'或是'o',分别代表偶校验和奇校验。输出正确

的01字符串。偶校验表示:算上最后的'e'位,总共有偶数个1。奇校验表示:算上最后

'o'位,1总共有奇数个。

思路:统计除末尾外1的个数,然后再判断末尾应为0还是1。然后赋值输出字符串。


#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;char s[40];int main(){    while(cin >> s && s[0]!='#')    {        int len = strlen(s),sum = 0;        for(int i = 0; i < len-1; ++i)            if(s[i] == '1')                sum++;        if(s[len-1]=='e' && sum%2==0)            s[len-1] = '0';        else if(s[len-1]=='o' && sum%2==0)            s[len-1] = '1';        else if(s[len-1]=='e' && sum%2==1)            s[len-1] = '1';        else if(s[len-1]=='o' && sum%2==1)            s[len-1] = '0';        cout << s << endl;    }    return 0;}


0 0