• 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);
        }
    }
  • 相关阅读:
    [PHP]socket的连接超时 与 读取/写入超时
    [PHP]引用返回与节省内存
    [PHP]实体类基类和序列化__sleep问题
    [PHP]日志处理error_log()函数和配置使用
    [PHP] 使用反射实现的控制反转
    [PHP] debug_backtrace()可以获取到代码的调用路径追踪
    [TCP/IP] TCP的传输连接管理
    [PHP] sys_get_temp_dir()和tempnam()函数报错与环境变量的配置问题
    [PHP] ubuntu下使用uuid扩展获取uuid
    [Linux] host dig nslookup查询域名的DNS解析
  • 原文地址:https://www.cnblogs.com/kimsimple/p/6711469.html
Copyright © 2020-2023  润新知