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);
        }
    }
  • 相关阅读:
    百度音乐搜索API
    浅谈对设计模式的理解
    AS3 Embed
    AS3.0中遍历删除容器内子对象的误区。
    如何获取GridView的总记录数?
    分享几个国外的Ajax&Jquery网站
    SQL2000: MMC 不能打开文件
    结合.net开发 谈谈 access 中 模糊查询语句 like的用法
    [转]C#里巧用DateTime预设一些可选的日期范围(如本年度、本季度、本月等)
    根据条件动态改变GridView某行或某个单元格的背景色
  • 原文地址:https://www.cnblogs.com/kimsimple/p/6711469.html
Copyright © 2011-2022 走看看