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
  • 相关阅读:
    分页参数处理逻辑的最佳实践
    浅谈软件界面设计原则
    Django 页面缓存的cache_key是如何生成的
    mvn 命令上传 jar 包到 nexus 私仓
    知 识 收 录
    JavaScript 使用Map对象
    windows bat脚本守护java进程
    ubuntu java启动shell脚本
    Linux cron定时任务启动jar程序
    ubuntu java调用海康sdk报错Unable to load library '/home/bjlthy/HCNetSDK/libPlayCtrl.so'
  • 原文地址:https://www.cnblogs.com/gpsx/p/5197870.html
Copyright © 2011-2022 走看看