An easy problem (位运算)

【题目描述】

    给出一个整数,输出比其大的第一个数,要求输出的数二进制表示和原数二进制表示下1的个数相同。

【题目链接】

    http://noi.openjudge.cn/ch0406/1455/

【算法】

    1、自己想的:设原数为n,从lowbit(n)开始左移找到第一个0的位置,同时记录该位置之前1的个数,将该位置置1,然后把1全堆在最后;如果找不到该位置,则该数是形如111100000...的样式,故将其左移一位,再把1堆在最后。感觉不够清晰。

    2、借鉴网上题解,比我清晰很多:直接给原数加上lowbit(n),再把1堆在最后,结束。。。而且堆在最后也可以简洁的用位运算:(n^(n+lowbit(n)))/lowbit(n)>>2(原来lowbit(n)前有x个1,异或之后有x+1个1,应该堆x-1个1,所以右移两位)。。。不过用时都是2ms。。。时间都耗在cin上了吧。。。。

【代码1】

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int a,b,rec,num,ans;
 4 int main()
 5 {
 6     while(cin>>a&&a) {
 7         rec=num=0;
 8         b=a&-a;
 9         while(b<=a&&!rec) {
10             if(!(a&b)) rec=b;
11             else num++;
12             b<<=1;
13         }
14         num--;
15         if(!rec) {
16             ans=b+(1<<num)-1;
17         }
18         else {
19             rec=-rec;
20             ans=a&rec;
21             ans+=(-rec);
22             ans+=(1<<num)-1;
23         }
24         cout<<ans<<endl;
25     }
26     return 0;
27 }

【代码2】

#include <bits/stdc++.h>
using namespace std;
int a;
int main()
{
    while(cin>>a&&a) {
        cout<<a+(a&-a)+((a^(a+(a&-a)))/(a&-a)>>2)<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Willendless/p/9349798.html

时间: 2024-07-30 16:40:23

An easy problem (位运算)的相关文章

[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 min

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): 这样防止了数据的溢出.

ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Practice Description Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: Operation 1: AND opn L R Here opn, L and R are integer

位运算+枚举

位运算http://c.biancheng.net/cpp/html/101.html 在很多情况下(比如翻棋子)在搜索时涉及大量枚举往往导致超时,位运算则很好地解决了这个问题,方便又快捷 HDU  1882   Strange Billboard http://acm.hdu.edu.cn/showproblem.php?pid=1882 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot

位运算和关于两个数交换的多种方法

我们知道,位运算在计算中有着广泛的应用. 在计算机的各种编程语言中位运算也是一种不可缺少的运算,尤其是在计算机的底层实现代码中. 以下我们就来介绍一下位运算. 1.左移运算<<  左移右移都是移动二进制数 0000-0000 0000-0000 0000-0000 0000-1100     =12 向左移动一位变为(右边缺几位就补几个0) 0000-0000 0000-0000 0000-0000 0001 1000       =24 再向左移一位 0000-0000 0000-0000

an easy problem(贪心)

An Easy Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8333   Accepted: 4986 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.

POJ 2777 Count Color(线段树+位运算)

题目链接:http://poj.org/problem?id=2777 Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a

位运算之——按位与(&amp;)操作——(快速取模算法)

由于位运算直接对内存数据进行操作,不需要转成十进制,因此处理速度非常快. 按位与(Bitwise AND),运算符号为& a&b 的操作的结果:a.b中对应位同时为1,则对应结果位也为1. 例如: 10010001101000101011001111000 & 111111100000000 --------------------------------------------- 10101100000000 对10101100000000进行右移8位得到的是101011,这就得