lightoj 1042 - Secret Origins
来源:互联网 发布:linux socket调试工具 编辑:程序博客网 时间:2024/05/16 06:13
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
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.
题意就是求与n二进制中1的个数相同的却比n大的最小的数。在n的二进制数中第一个出现01的,改为10,后面的1全部移到最后面
#include<stdio.h>
#include<string.h>
int main()
{
long long sum,next;
int a[100],n,i;
int t;scanf("%d",&t);
int s=1;
while(t--)
{
memset(a,0,sizeof(a));
scanf("%d",&n);
int l=0;
while(n)
{
a[l++]=n%2;
n/=2;
}
int ans=0;
for(i=0;i<l;i++)
{
if(a[i]==1&&a[i+1]==0)
{
next=i;
break;
}
if(a[i]==1)
ans++;
}
for(i=0;i<next;i++)
{
if(i<ans)
a[i]=1;
else
a[i]=0;
}
a[next]=0;
a[next+1]=1;
sum=0;
long long an=1;
for(i=0;i<=l;i++)
{
sum+=an*a[i];
an*=2;
}
printf("Case %d: %lld\n",s++,sum);
}
return 0;
}
- LightOJ 1042 - Secret Origins
- LightOJ 1042 Secret Origins
- lightoj 1042 - Secret Origins
- lightoj 1042 - Secret Origins 【数学】
- LightOJ 1042 Secret Origins 贪心
- LightOJ 1042 Secret Origins(二进制、STL)
- 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
- light oj 1042 Secret Origins
- Secret Origins
- 1042 - Secret Origins (位运算好题)
- LightOJ1042 - Secret Origins (位运算)
- <LightOJ 1338> Hidden Secret!
- 整理ORACLE官网Linux X86-64系统安装步骤
- 象棋机器人 1 数据分析收集-爬虫编写
- DNS
- Gradle的安装与使用
- iOS UIwebview的加载
- lightoj 1042 - Secret Origins
- Scala-"_"的用法
- Anroid studio遇到的问题7--- findViewById 返回为null (自定义控件)
- springmvc彻底解决中文乱码问题
- 80. Remove Duplicates from Sorted Array II
- 树形DP整理小结
- 使用navigator.userAgent.toLowerCase()判断登陆端是pc还是手机
- 如何画分割线
- 一个简单的一致性哈希算法实现(Java版本)