An Easy Problem

题目的汉译在题目的最后,但是希望各位读者尽量只看英文题目,以提高自己的能力!
总时间限制: 1000ms 内存限制: 65536kB

描述

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”.

输入

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.

输出

One integer per line, which is J.

样例输入

1
2
3
4
78
0

样例输出

2
4
5
8
83

来源

POJ Monthly,zby03

代码实现(思路在代码内)

#include <iostream>

using namespace std;
int number(int a)//写一个专门的函数来计算整数转换为二进制后1的个数
{
    int ans=0;//来记录1的数量
    while(a>0)//辗转相除法的条件,且条件不能为 a!=0
    {
        if(a%2==1)
        {
            ans++;
        }
        a/=2;
    }//辗转相除的过程
    return ans;
}

int main()
{   int x;
    while(cin>>x&&x!=0)//循环输入且在输入0时结束
    {
        int temp=x;//用temp记录每个x的初值
        while(1)
        {   x++;//从x时逐个累加,寻找最小的符合条件的整数
            if(number(x)==number(temp))
            {
                cout<<x<<endl;
                break;
            }
        }

    }
}


01:一个简单的问题
总时间限制: 1000 ms 内存限制: 65536 kB
描述
正如我们所知,存储在计算机中的数据是二进制形式的。我们现在讨论的问题是正整数及其二进制形式。

给定一个正整数i,您的任务是找出一个整数J,它是大于i的最小整数,其二进制形式与I的二进制形式相同的‘1’s的数目。

例如,如果给出“78”,我们可以写出它的二进制形式“1001110”。这个二进制形式有4‘1’s,最小整数大于“1001110”,也包含4‘1’s,是“1010011”,即“83”,所以你应该输出“83”。
输入
每一行一个整数,即i(1<=i<=1000000)。

包含数字“0”的行终止输入,这行不需要处理。
输出
每一行一个整数,即J。
样例输入
1
2
3
4
78
0
样例输出
2
4
5
8
83
来源
POJ月刊,ZBY 03

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值