求兩個整數之間的漢明距離---Salem

來源:互聯網
上載者:User

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 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.

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

題目大意:
給你任意個整數,然後求出這些整數中漢明距離最大的一對數並輸出距離。
解題思路:
所謂漢明距離就是給定兩個相同長度的字串,找出字串中相同位置不同值的元素個數。
在這個題目中使用到二進位的異或運算,因為異或的規則是兩個數相同為0,不同為1,所以只需將兩個數進行異或運算再求出其結果整數的位元中1的個數即可。

求一個整數其位元中1的個數可以使用快速法:

int BitCount(int n){     int c =0 ;    for (c =0; n; ++c)    {        n &= (n -1) ; // 清除最低位的1    }    return c;}

具體代碼如下:

#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) ; // 清除最低位的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;}

僅代表個人觀點,歡迎交流探討,勿噴~~~

PhotoBy:WLOP

http://weibo.com/wlop

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.