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);
        }
    }
  • 相关阅读:
    clientWidth和offsetWidth区别 e.pageX和e.clientX区别
    vue笔记(七)组件的生命周期
    vue笔记(七)网络封装
    vue笔记(六)自定义消息弹出
    vue笔记(五)插槽. 路由
    组件中的样式问题,穿透和scale占位, 引入静态资源
    python基础-迭代器
    python基础-装饰器
    python基础-函数对象和闭包
    python基础-名称空间与作用域
  • 原文地址:https://www.cnblogs.com/kimsimple/p/6711469.html
Copyright © 2011-2022 走看看