[POJ] 2453 An Easy Problem [位运算]

An Easy Problem

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

题解:输入一个整数x,求一个数y,使得y的二进制各位上1的个数和等于x的二进制各位上1的个数和,y>x,y尽量小。直接每次加1,统计各位上1的个数,每次求y末尾是否为1,直接判断y是否为奇数即可,然后y>>=1,右移一位。

代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3
 4 using namespace std;
 5
 6 int main()
 7 {
 8     int p,x,num1,num2;
 9
10     while(1) {
11         num1=0;num2=0;
12         scanf("%d",&x);
13         if(!x) break;
14         p=x;
15         while(p>0) {
16             if(p%2!=0) num1++;
17             p>>=1;
18         }
19         while(1) {
20             x++;
21             p=x;
22             num2=0;
23             while(p>0) {
24                 if(p%2!=0) num2++;
25                 p>>=1;
26             }
27             if(num2==num1) {
28                 printf("%d\n",x);
29                 break;
30             }
31
32         }
33     }
34
35     return 0;
36 }
时间: 2024-10-17 10:19:01

[POJ] 2453 An Easy Problem [位运算]的相关文章

An easy problem (位运算)

[题目描述] 给出一个整数,输出比其大的第一个数,要求输出的数二进制表示和原数二进制表示下1的个数相同. [题目链接] http://noi.openjudge.cn/ch0406/1455/ [算法] 1.自己想的:设原数为n,从lowbit(n)开始左移找到第一个0的位置,同时记录该位置之前1的个数,将该位置置1,然后把1全堆在最后:如果找不到该位置,则该数是形如111100000...的样式,故将其左移一位,再把1堆在最后.感觉不够清晰. 2.借鉴网上题解,比我清晰很多:直接给原数加上lo

An easy problem(位运算)

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 intege

POJ 1152 An Easy Problem! (取模运算性质)

题目链接:POJ 1152 An Easy Problem! 题意:求一个N进制的数R,保证R能被(N-1)整除时最小的N. 第一反应是暴力.N的大小0到62.发现其中将N进制话成10进制时,数据会溢出.这里有个整除,即(N-1)取模为0. 例子:a1a2a3表示一个N进制的数R,化成10进制: (a1*N*N+a2*N+a3)%(N-1)==((a1*N*N)%(N-1)+(a2*N)%(N-1)+(a3)%(N-1))%(N-1)==(a1+a2+a3)%(N-1): 这样防止了数据的溢出.

POJ 2826 An Easy Problem? 判断线段相交

POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客 下面三种情况比较特殊,特别是第三种 G++怎么交都是WA,同样的代码C++A了 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const double eps = 1e-8;

poj 2799 IP Networks 模拟 位运算

poj链接:http://poj.org/problem?id=2799 这题实在是非常的有趣... 写的时候也非常的开心... 然后就写跪了... 刚好讲了ip地址和子网掩码的只是 整个学期的通信导论我就只有这节课有事没去结果昨晚把这方面的只是补起来了有种功德圆满的感觉 network address是前(32-n)随意 后n位全零 network mask是前(32-n)全一 后n位全零 其次是练习了各种移位操作 我发现移位操作是个非常好用的东西 因为它自填充0 所以对一个二进制数往右移8位

Poj 2826 An Easy Problem?!

地址:http://poj.org/problem?id=2826 题目: An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13016   Accepted: 2003 Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Ben nails tw

POJ 2826 An Easy Problem?!(计算几何)

An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10533   Accepted: 1589 Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Ben nails two wooden boards on the wall of his bar

POJ 2826 An Easy Problem?!(线段相交,分类讨论)

题意:给两个线段,问他们能收集到多少雨水. 链接:http://poj.org/problem?id=2826 解法:分四种情况讨论 1. 存在一个线段与x轴平行,答案为0 2. 两个线段没有交点,答案为0 3. 1和2都不满足时,令线段1为比较低的那个线段,且p1为其比较高的那个点,若该点往y轴正方向的射线与线段2有交点,则答案为0 4. 3不满足时,求出两线段交点x1,p1做一条平行于x轴的线,该线与线段2的交点x2,则三角形x1, x2, p1的面积就是答案 小结:此题属于分类讨论型的题,

POJ 2826 An Easy Problem!(简单数论)

Description Have you heard the fact "The base of every normal number system is 10" ? Of course, I am not talking about number systems like Stern Brockot Number System. This problem has nothing to do with this fact but may have some similarity. Y