LightOJ 1042 Secret Origins【位运算】
来源:互联网 发布:淘宝的同仁堂是真的吗 编辑:程序博客网 时间:2024/06/04 08:38
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
题意:
给出一个数字,让你求大于这个数,而且二进制中1的个数和这个数的相等的最小的数。
题解:
不知道怎么写题解了,确实没做出来,只想到了一部分怎么做,然后没能够实现,找了大神的做法,瞬间感觉自己什么都不会......
#include<cstdio>#include<cstring>#include<cmath>using namespace std;typedef long long ll;int main(){ll t;//freopen("shuju.txt","r",stdin);scanf("%lld",&t);for(ll k=1;k<=t;++k){ll n;scanf("%lld",&n);ll x=n&-n,y=n+x;n=((n&~y)/x>>1)|y;printf("Case %lld: %lld\n",k,n);}return 0;}
贪心的思路是可以解决的:
找到最后的一个1,然后向前找第一个0,找到后,向后找第一个1,两者交换位置,然后把找到0,然后把这个1 之后的所有的1 都放在最后边,得到的数就是需要的数....
(纸上谈兵...)
- LightOJ 1042 Secret Origins【位运算】
- lightoj 1042 Secret Origins(位运算符号的应用)
- LightOJ 1042 - Secret Origins
- LightOJ 1042 Secret Origins
- lightoj 1042 - Secret Origins
- lightoj 1042 - Secret Origins 【数学】
- LightOJ 1042 Secret Origins 贪心
- 1042 - Secret Origins (位运算好题)
- LightOJ1042 - Secret Origins (位运算)
- LightOJ 1042 Secret Origins(二进制、STL)
- LightOJ - 1042 Secret Origins (模拟)水
- LightOJ 1042 Secret Origins(贪心)
- LightOJ-1042---Secret Origins (贪心+暴力)
- LightOJ 1042 - Secret Origins(数论&进制转换)
- 1042 - Secret Origins
- light oj 1042 Secret Origins
- Secret Origins
- lightoj 1042 经典的位运算
- Java模块 -- BigDecimal 支持任何精度的定点数
- 圆形头像
- eMule 的使用场景及初始化任务描述级eMule相关代码分析(仅涉及ed2k)
- 使用负载均衡之后的Session处理思路
- vtk实战(三十)——写入vtu数据
- LightOJ 1042 Secret Origins【位运算】
- JDK动态代理示例
- ACM--BFS--图--nyoj 20--吝啬的国度
- Hadoop内置的数据输入\输出格式与RecordReader\RecordWriter(九)
- 网页缩放
- 【LeetCode】Perfect Squares 解题报告
- 应用层数据库读写分离--续
- java语法break、continu、label语句
- nova boot代码流程分析(三):nova与neutron的plugin交互