zoukankan      html  css  js  c++  java
  • lightoj-1042

    1042 - Secret Origins
    PDF (English) Statistics Forum
    Time Limit: 0.5 second(s) Memory Limit: 32 MB
    This is the tale of Zephyr, the greatest time traveler the world will never know. Even those who are aware of Zephyr's existence know very little about her. For example, no one has any clue as to which time period she is originally from.

    But we do know the story of the first time she set out to chart her own path in the time stream. Zephyr had just finished building her time machine which she named - "Dokhina Batash". She was making the final adjustments for her first trip when she noticed that a vital program was not working correctly. The program was supposed to take a number N, and find what Zephyr called its Onoroy value.

    The Onoroy value of an integer N is the number of ones in its binary representation. For example, the number 13 (11012) has an Onoroy value of 3. Needless to say, this was an easy problem for the great mind of Zephyr. She solved it quickly, and was on her way.

    You are now given a similar task. Find the first number after N which has the same Onoroy value as N.

    Input
    Input starts with an integer T (≤ 65), denoting the number of test cases.

    Each case begins with an integer N (1 ≤ N ≤ 109).

    Output
    For each case of input you have to print the case number and the desired result.

    Sample Input
    Output for Sample Input
    5
    23
    14232
    391
    7
    8
    Case 1: 27
    Case 2: 14241
    Case 3: 395
    Case 4: 11
    Case 5: 16

    题目大意: 给你一个n,求出大于n但是二进制的1的个数跟n相同的数。

    例如: 010110->011001

    通过观察我们发现只要出现第一01时把01改成10  并把次01后面的1都压到最后面就可以了

    例如01(01)(111100)->01(10)(001111), 通过这样就可以得出答案。

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    int main(){
        
        int T,n,save;
        scanf("%d",&T);
        for(int t=1;t<=T;t++){
            
            scanf("%d",&n);
            save = 0;
            for(int i=1;i<=30;i++){            
                if(n&1){
                    save++; //记录出现了多少个1 
                    if(!((n>>1)&1)){
                        n = (n>>1)+1;
                        n = n<<(i);//cout<<n<<" "<<save<<endl;
                        for(int j=0;j<save-1;j++){
                            n = n|(1<<j);
                        }
                        break;
                    }
                }
                n = n>>1;
            }
            printf("Case %d: %d
    ",t,n);
            
        }
        
        return 0;
    } 
  • 相关阅读:
    Android Matrix(坐标矩阵)
    Android 修改底部导航栏navigationbar的颜色
    Java跨平台原理
    Java的平台无关性
    AudioManager --- generateAudioSessionId
    Android中使用logwrapper来重定向应用程序的标准输出
    Linux指令
    HLS -- m3u8档案格式解析
    新購電腦筆記
    Android多媒体--MediaCodec 中文API文档
  • 原文地址:https://www.cnblogs.com/yuanshixingdan/p/5543917.html
Copyright © 2011-2022 走看看