SalemGym - 100814I

xiaoxiao2021-02-28  71

                            Salem

  Gym - 100814I 

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

Example Input 2 2 12 5 3 1 2 3 Output 2 2

题目大意:两个数的hanming距离是两个数的二进制相应位置的数相同的个数。题目给一组数,求这一组数的最大hanming距离。

求hanming距离时要用到   ^ (异或),两个数的二进制相同的位置为1不同为0。还有  a=a&(a-1)  可以将a最后一位的1消除。例如:11111&11110结果为11110,11110&11101结果为11100,11100&11011结果为11000...........这样就可以知道一个数二进制有多少个1。

#include<stdio.h> int judge(int x) { int count=0; while(x){//计算x的二进制有多少个1 x=x&(x-1); count++; } return count; } int main() { int T,a[105],t; scanf("%d",&T); while(T--){ int n; scanf("%d",&n); int max=0; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){//两个数的二进制相同的位置的数相同为1不同为0 t=judge(a[i]^a[j]); if(t>max) max=t; } } printf("%d\n",max); } }

转载请注明原文地址: https://www.6miu.com/read-59800.html

最新回复(0)