zoukankan      html  css  js  c++  java
  • HUST 1404 Hamming Distance(字符串)

    Hamming Distance

    Description

    Have you ever heard of the Hamming distance. It is the number of positions for which the corresponding digits differ. Your task is to write a program that computes this distance for two binary strings.

    Input

    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".

    Output

    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.  

    Sample Input

    0
    1
    000
    000
    1111111100000000
    0000000011111111
    101
    000
    X

    Sample Output

    Hamming distance is 1.
    Hamming distance is 0.
    Hamming distance is 16.
    Hamming distance is 2.

    题解:读取两个相同长度字符串,从第一位往后对比,如果不一样,ans++。X结束。

    #include <cstdio>
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <cstring>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #define PI acos(-1.0)
    #define ms(a) memset(a,0,sizeof(a))
    #define msp memset(mp,0,sizeof(mp))
    #define msv memset(vis,0,sizeof(vis))
    using namespace std;
    //#define LOCAL
    int main()
    {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
    #endif // LOCAL
        ios::sync_with_stdio(false);
        char a[120],b[120];
        while(cin>>a&&a[0]!='X')
        {
            cin>>b;
            int ans=0;
            for(int i=0,s=strlen(a);i<s;i++)
            {
                if(a[i]!=b[i])ans++;
            }
            printf("Hamming distance is %d.
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    3. 23 模拟面试
    3.15 模拟面试
    C++ 浅谈virtual
    3.6 模拟面试
    为s5pv210烧录镜像
    HISI VENC 实际输出帧率控制
    live555 交叉编译移植到海思开发板
    雄迈取流
    面试官吐槽:“软件测试员就是不行!”网友:我能把你面哭了!——软件测试笔试面试题目完全汇总
    “女人~,你在玩火”一个有磁性的声音说道——常用自动化测试工具
  • 原文地址:https://www.cnblogs.com/gpsx/p/5197870.html
Copyright © 2011-2022 走看看