题目:
输入一个字符串,然后对每个字符进行奇校验,最后输出校验后的二进制数(如'3’,输出:10110011)。
输入描述:
输入包括一个字符串,字符串长度不超过100。输出描述:
可能有多组测试数据,对于每组数据, 对于字符串中的每一个字符,输出按题目进行奇偶校验后的数,每个字符校验的结果占一行。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
char[] str = in.next().toCharArray();
for(int i = 0; i < str.length; i++){
int tmp = str[i]; //字符串中每个字符
int[] res = new int[9]; //每个字符校验后的结果
int count = 0, index = 0;//1的个数
while(tmp != 0){
res[index++] = tmp%2;
if(tmp%2 == 1)
count++;
tmp = tmp/2;
}
while(index < 7){
res[index++] = 0;
}
if(count%2 == 0){
res[index] = 1;
}else{
res[index] = 0;
}
//输出
for(int j = index; j >=0; j--){
System.out.print(res[j]);
}
System.out.println();
}
}
}
}