hdu 5142 NPY and FFT(二进制处理)

来源:互联网 发布:淘宝无线端链接地址 编辑:程序博客网 时间:2024/04/29 13:13

NPY and FFT

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


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
3681
 

Sample Output
311
 

Source
BestCoder Round #22
题意:把n转换为二进制再翻转,去掉前导零后的数是多少

思路:用数组保存二进制,处理前导零即可

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;int a[50],b[50];int main(){    int T,n;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        int cnt=0;        while(n)        {            int t=n%2;            a[cnt++]=t;            n/=2;        }        int num;        for(num=0; num<cnt; num++)            if(a[num]) break;        int tot=0;        for(int i=cnt-1; i>=num; i--)            b[tot++]=a[i];        int ans=0;        for(int i=0; i<tot; i++)        {            if(b[i])                ans=ans+(1<<i);        }        printf("%d\n",ans);    }    return 0;}




0 0
原创粉丝点击