zoukankan      html  css  js  c++  java
  • codeforces gym/100814 humming distance (二进制位数比较)

     Gym - 100814I 

    I. Salem
    time limit per test
    1 second
    memory limit per test
    1024 megabytes
    input
    standard input
    output
    standard output

    Salem is known to be one of the best competitive programmers in the region. However, he always finds a hard time understanding the concept of the hamming distance. The hamming distance of two numbers is defined as the number of different digits in their binary representations (leading zeros are used if necessary to make the binary representations have the same length). For example the hamming distance between 12 and 5 is 2 since their binary representations are 1100 and 0101 respectively and they differ in the first and fourth positions.

    Recently, Salem got a problem that he needs your help to solve. He is given N integers and asked to get the maximum among the hamming distances of all possible pairs of the given integers.

    Input

    The first line of the input will be a single integer T representing the number of test cases. Followed by T test cases. Each test case will start with a line with single integer (2 ≤ N ≤ 100) representing the number of the integers. Each of the following N lines contains one of the integers (1 ≤ Ai ≤ 10, 000) from the list of the integers.

    Output

    For each test case print one line consists of one integer representing the maximum hamming distance between all possible pairs from the given integers.

    Examples
    input
    2
    2
    12
    5
    3
    1
    2
    3
    output
    2
    2

    参考:
    http://codeforces.com/blog/entry/21567

    两种方法:
    1.
    int humming(int x,int y)

    2.
    __builtin_popcount(arr[i]^arr[j]);/

    #include "cstdio"
    #include "iostream"
    using namespace std;
    int humming(int x,int y)
    {
        int ans=0;
        while(1)
        {
            if(x==0&&y==0)break;
            if(x%2!=y%2)
            ans++;
            x/=2;y/=2;
        }
        return ans;
    }
    int main()
    {
        int a,b;
        int t,n;
        int x,y;
        int arr[105];
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(int i=0;i<n;i++)
            {
                scanf("%d",&arr[i]);
            }
            int dist=0,max1=0;
            for(int i=0;i<n;i++)
            {
                for(int j=i;j<n;j++)
                {
                    //dist=__builtin_popcount(arr[i]^arr[j]);///求两数最大海明差距
                    dist=humming(arr[i],arr[j]);
                    max1=max1>dist?max1:dist;
                }
            }
            printf("%d
    ",max1);
        }
    }
  • 相关阅读:
    storm环境搭建
    环境变量
    vmware tools安装及使用其实现与宿主机共享文件夹
    关于mysql中的count()函数
    centos7配置jdk8
    linux常用命令(随时更新中)
    ES中对索引的相关操作
    linux centos7下源码 tar安装mysql5.7.23(5.7以上均可试用)
    安装linux虚拟机配置静态ip(桥接模式,外部机器能够访问)
    安装linux虚拟机配置静态ip(NAT模式)
  • 原文地址:https://www.cnblogs.com/kimsimple/p/6711469.html
Copyright © 2011-2022 走看看