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);
        }
    }
  • 相关阅读:
    使用VIRTUALBOX安装ANDROID系统 | 图文教程 | 相关设置
    android-x86 下载地址
    java 返回某一天的周日和现在这一周的周日
    使用github上的开源框架SlidingMenu环境的搭建,以及getSupportActionBar方法不能找到的问题
    UI 组件
    git 初始化
    Android,visibility属性
    android去掉标题栏
    error opening trace file: No such file or directory (2) ,can't load transform_config.xml
    Ubuntu下解压缩文件
  • 原文地址:https://www.cnblogs.com/kimsimple/p/6711469.html
Copyright © 2011-2022 走看看