UPC 2017 Summer Training 1

I - Salem


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
 

题目的大意是:找到2个数字然后转化成二进制,也就是01组成的串,然后查找相同的位置对应不同的数最多的数;
题意给的是5和12   5转化后是101  12转化后是1100  位数不足补零        最后是0101和1100比较 结果是2
数据量只有100 2层for循环
#include<stdio.h>
#include<string.h>
int max(int a,int b)
{
	return a>b?a:b;
}
int p(int aa,int bb)//判断比较 简单暴力
{
	int sum=0;
	while(aa!=0 || bb!=0)
	{
		int temp=aa%2;
		aa=aa/2;
		int temp1=bb%2;
		bb=bb/2;
		if(temp!=temp1)
		{
			sum++;
		}
	}
	return sum;
} 
int main()
{
	int maxx,sum;
	int n,m;
	int l[2000];
	while(scanf("%d",&n)!=EOF)
	{
		while(n--)
		{
			scanf("%d",&m);
			sum=0;
			for(int i=0;i<m;i++)
				scanf("%d",&l[i]);
			for(int i=0;i<m-1;i++)
			{
				for(int j=i+1;j<m;j++)
				{
					maxx=p(l[i],l[j]); 
					sum=max(sum,maxx);
				}
			}
			printf("%d\n",sum);
		}
	}
	return 0;
 } 
PS:开全局数组传递数值,别传递坐标,改变值~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值