hdu2700Parity(水题)

来源:互联网 发布:js正则验证用户名 编辑:程序博客网 时间:2024/05/04 06:40

Parity

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


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


题意:在最后一个字符前算出每一位的和sum最后一位判断sum的奇偶性 e表示判断sum是偶数o表示判断sum是奇数若判断成立则输出1 反之输出0


code:

#include <stdio.h>#include <string.h>char a[100];int main(){int i,l,sum;while(scanf("%s",a)&&a[0]!='#'){sum=0;l=strlen(a);for(i=0;i<l-1;i++){sum+=a[i]-'0';printf("%c",a[i]);}if((sum%2==0&&a[l-1]=='e')||(sum%2==1&&a[l-1]=='o')){printf("0\n");}else{printf("1\n");}}return 0;}



0 0
原创粉丝点击