zoukankan      html  css  js  c++  java
  • An Easy Problem

    Time Limit: 
    1000ms
     
    Memory Limit: 
    65536kB
    Description
    As we known, data stored in the computers is in binary form. The problem we discuss now is about the positive integers and its binary form.

    Given a positive integer I, you task is to find out an integer J, which is the minimum integer greater than I, and the number of '1's in whose binary form is the same as that in the binary form of I.

    For example, if "78" is given, we can write out its binary form, "1001110". This binary form has 4 '1's. The minimum integer, which is greater than "1001110" and also contains 4 '1's, is "1010011", i.e. "83", so you should output "83".
    Input
    One integer per line, which is I (1 <= I <= 1000000).

    A line containing a number "0" terminates input, and this line need not be processed.
    Output
    One integer per line, which is J.
    Sample Input
    1
    2
    3
    4
    78
    0
    
    Sample Output
    2
    4
    5
    8
    83
    
    Source
      POJ Monthly,zby03
    题意:给定多组测试数据,每个数据一个正整数N,如果把N换算成二进制,如果有x个1,如78的二进制有4个1,那么求一个数,使得该数比x大,且换算成二进制的话也有x个1,求这个数的最小值。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<cstring>
     7 using namespace std;
     8 int N;
     9 int a[100];
    10 int binum[100];
    11 inline void change(){
    12     memset(a,0,sizeof(a));
    13     memset(binum,0,sizeof(binum));
    14     int tmp=N;
    15     int now=1;
    16     while(tmp/2>0){
    17         a[now]=tmp%2;
    18         tmp/=2;
    19         now++;
    20     }
    21     a[now]=1;
    22     a[0]=now;
    23 }
    24 inline void move(){
    25     int i=1;
    26     int cnt=0;
    27     while(a[i]!=1||a[i+1]!=0){
    28         if(a[i]==1) cnt++;
    29         i++;
    30     }
    31     a[i+1]=1;
    32     for(int k=1;k<=i;k++) a[k]=0;
    33     for(int k=1;k<=cnt;k++) a[k]=1;
    34 }
    35 inline void calc(){
    36     int ANS=0; 
    37     int base=1;
    38     for(int j=1;j<=a[0]+1;j++){
    39         ANS+=a[j]*base;
    40         base=base*2;
    41     }
    42     cout<<ANS<<endl;
    43 }
    44 int main(){
    45     for(;;){
    46         scanf("%d",&N);
    47         if(N==0) break;
    48         change();
    49         move();
    50         calc();            
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    函数的重载 C++快速入门06
    PE格式详细讲解8 系统篇08|解密系列
    《零基础入门学习汇编语言》检测点,实验,课后题答案
    PE格式详细讲解9 系统篇09|解密系列
    C++输出输入小结 C++快速入门05
    使用XML生成菜单
    DNS解析过程详解
    Windows Azure 2.5天深度技术训练营 和 微软公有云发现之旅
    使用单例模式实现自己的HttpClient工具类
    android 反编译和防止被反编译。
  • 原文地址:https://www.cnblogs.com/CXCXCXC/p/4924198.html
Copyright © 2011-2022 走看看