To find the Hamming distance between two integers---Salem

Source: Internet
Author: User
Tags time limit

B-salem
Time limit:1000ms Memory limit:1048576kb 64bit IO format:%i64d &%i64u
Submit

Status

Practice

Gym 100814I
Description
Standard Input/output
Statements
Salem is known to being one of the best competitive programmers in the region. However, he finds a hard time understanding the concept of the Hamming distance. The Hamming distance of the numbers is defined as the number of different digits in their binary representations (leading Zeros is used if necessary to make the binary representations has the same length). For example the Hamming distance between and 5 was 2 since their binary representations is 1100 and 0101 respectively a nd 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 integer S.

Input
The first line of the input is a single integer T representing the number of test cases. Followed by T test cases. Each test case would 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, $) 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 p Airs from the given integers.

Sample Input
Input
2
2
12
5
3
1
2
3
Output
2

Main topic:
Give you any integer, and then find the number of the most hamming distance in these integers and the output distance.
Problem Solving Ideas:
The so-called Hamming distance is given two strings of the same length, to find the number of elements in the same position in the string with different values.
In this topic to use the XOR operation of the binary, because the rule of the XOR is two number is the same as 0, the difference is 1, so we just need to make the two number of different or operation to find the result of the integer number 1 in the binary number.

You can use the fast method to find the number of 1 in the binary number of an integer:

int bitcount (int n)
{
     int c =0;
    for (c =0; n; ++c)
    {
        n &= (n-1);//clears the lowest bit of 1
    }
    return c;
}

The specific code is as follows:

#include <iostream>
#include <cstdio>
#include <cstring> 
using namespace std;
const int maxn=105;
int A[MAXN];
int bitcount (int n)
{
     int c =0;
    for (c =0; n; ++c)
    {
        n &= (n-1);//clears the lowest bit of 1
    }
    return c;
}
int main ()
{
    int T;
    cin>>t;
    int N;
    while (t--)
    {
        int max=0;
        memset (a,0,sizeof (0));
        cin>>n;
        scanf ("%d", &a[0]);
        for (int i=1;i<n;i++)
        {
            scanf ("%d", &a[i]);
            for (int j=0;j<i;j++)
            {
                int maxw=bitcount (a[i]^a[j]);
                if (Maxw>max)
                {
                    max=maxw;}}
        }
        cout<<max<<endl;
    } 
    return 0;
}

Only on behalf of personal views, welcome to Exchange discussion, do not spray ~ ~ ~

Photoby:wlop

Http://weibo.com/wlop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.