题目描述
你听说过汉明距离吗?它是相应数字不同的位置数。您的任务是编写一个程序来计算两个二进制字符串的此距离。
输入
The input contains several test cases. Each test case consists of two lines. Each line contains one binary number. Any two numbers given in one test case have the same length, which is at most 100 binary digits. The last test case is followed by a line containing the uppercase letter “X”.
输出
Your program must output a single line for each test case. The line should contain the statement “Hamming distance is X.”, where X is the number of positions where the two numbers have different digits.
样例输入
0 1 000 000 1111111100000000 0000000011111111 101 000 X样例输出
Hamming distance is 1. Hamming distance is 0. Hamming distance is 16. Hamming distance is 2.#include<stdio.h> #include<string.h> int main() { char a[100]; char c[100]; while (scanf("%s",a) != EOF) { if (strcmp(a, "X") == 0) { break; } scanf("%s", c); int i; int len1 = strlen(a); int count = 0; for (i = 0; i < len1; i++) { if (a[i] != c[i]) { count++; } } printf("Hamming distance is %d.\n", count); } return 0; }