HDU 2700 Parity (水题)

来源:互联网 发布:数学英语词典知乎 编辑:程序博客网 时间:2024/05/18 01:31

传送门:点击打开链接

Parity

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


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
101e010010o1e000e110100101o#
 

Sample Output
101001001011100001101001010
 
英语不好,题意当时没怎么懂。就是说1的个数有奇数个或偶数个,若结尾是e,则1有偶数个,若是m则1有奇数个,填充最后一位,要么是1,要么是0。

水题不解释。


代码实现:


#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;int main(){char a[50];int i,j,count;while(cin>>a){getchar();count=0;if(strcmp(a,"#")==0)break;int n=strlen(a);for(i=0;i<n-1;i++){cout<<a[i];if(a[i]=='1'){count++;}}if(a[n-1]=='o'&&count%2==1)cout<<'0'<<endl;else if(a[n-1]=='o'&&count%2==0)cout<<'1'<<endl;else if(a[n-1]=='e'&&(count%2==0))cout<<'0'<<endl;else if(a[n-1]=='e'&&count%2==1)cout<<'1'<<endl;}return 0;}


原创粉丝点击