现在的位置: 首页 > 综合 > 正文

HDU 5142 NPY and FFT 数的二进制倒转

2018年01月19日 ⁄ 综合 ⁄ 共 1098字 ⁄ 字号 评论关闭

NPY and FFT

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 187    Accepted Submission(s): 119

Problem Description
A boy named NPY is learning FFT algorithm now.In that algorithm,he needs to do an operation called "reverse".
For example,if the given number is 10.Its binary representaion is 1010.After reversing,the binary number will be 0101.And then we should ignore the leading zero.Then the number we get will be 5,whose binary representaion is 101.
NPY is very interested in this operation.For every given number,he want to know what number he will get after reversing.Can you help him?
Input
The first line contains a integer T — the number of queries (1T100).
The next T lines,each contains a integer X(0X2311),the
given number.
Output
For each query,print the reversed number in a separate line.
Sample Input
3 6 8 1
Sample Output
3 1 1
/*
HDU 5142 数的二进制倒转 
*/
#include<iostream>
#include<stdio.h>
using  namespace std;
int main()
{
	int t,i,j,num,k,ans,b,a[40];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&num);
		k=0;
		while(num)
		{
			if(num%2==1)
				a[k++]=1;
			else
				a[k++]=0;
			num/=2;
		}
		i=0;
		while(i<k)
		{
			if(a[i]==0)
				i++;
			else
				break;
		}
		b=1;
		ans=0;
		for(j=k-1;j>=i;j--)
		{
			if(a[j]==1)
				ans+=b;
			b*=2;	
		}
		printf("%d\n",ans);
	}
	
	
	
	
	return 0;
}

抱歉!评论已关闭.