描述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 普通的暴力可以过 比较慢就是了
1的个数不变,那就找交换规律,其实只要从后向前找01子串交换,相当于进位了,再将后面的1依次放到末尾就行了
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string>
using namespace std; int solve(int n)
{
int b[], ans = ;
memset(b, , sizeof(b));
int k = ;
while (n) {
b[k++] = n % ;
n /= ;
}
k++; int cnt = ;
for (int i = ; i < k; i++) {
if (b[i] && b[i+]) {
cnt++;
b[i] = ;
}
if (b[i] && !b[i+]) {
b[i] = ;
b[i+] = ;
break;
}
}
for (int j = ; j < cnt; j++)
b[j] = ; for (int i = k; i >= ; i--) {
ans = ans* + b[i];
}
return ans;
} int main()
{
//freopen("1.txt", "r", stdin); int n;
while (cin >> n && n) {
cout << solve(n) << endl;
} return ;
}
												

[openjudge] 1455:An Easy Problem 贪心的更多相关文章

  1. an easy problem(贪心)

    An Easy Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8333   Accepted: 4986 D ...

  2. NOI4.6 1455:An Easy Problem

    描述 As we known, data stored in the computers is in binary form. The problem we discuss now is about ...

  3. 1455:An Easy Problem

    传送门:http://noi.openjudge.cn/ch0406/1455/ /-24作业 //#include "stdafx.h" #include<bits/std ...

  4. 一本通 1223:An Easy Problem

    \[传送门qwq\] [题目描述] 给定一个正整数N,求最小的.比N大的正整数M,使得M与N的二进制表示中有相同数目的1. 举个例子,假如给定的N为78,其二进制表示为1001110,包含4个1,那么 ...

  5. UVA-11991 Easy Problem from Rujia Liu?

    Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...

  6. An easy problem

    An easy problem Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  7. UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)

    Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...

  8. POJ 2826 An Easy Problem?!

    An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7837   Accepted: 1145 ...

  9. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

随机推荐

  1. Android databinding 开发参考

    1,android studio添加databinding依赖需要注意事项: http://www.zhihu.com/question/33538477?sort=created 2, databi ...

  2. Algorithm: Sieve of Eratosthenes

    寻找比n小的所有质数的方法. 2是质数, 2*i都是质数,同样3是质数,3*i也都是质数 代码如下 int n; vector<, true); prime[] = prime[] = fals ...

  3. 计算机网络: IP地址,子网掩码,网段表示法,默认网关,DNS服务器详解

    楔子: 以Windows系统中IP地址设置界面为参考(如图1), IP地址, 子网掩码, 默认网关 和 DNS服务器, 这些都是什么意思呢? 学习IP地址的相关知识时还会遇到网络地址,广播地址,子网等 ...

  4. python源码安装的包的卸载

    python setup.py install安装的包如何卸载 在使用源码安装的过程中,记录安装文件细节,如: python setup.py install --record log 这时所有的安装 ...

  5. AiCloud 2.0 AT开发文档【转】

    本文转载自:http://wiki.ai-thinker.com/aicloud/docs/atdevelop AT指令一览表 AiCloud AT指令   指令 描述 AT+CLDSTART 启动云 ...

  6. action和servlet共存(转)

    转自:http://www.cnblogs.com/nayitian/archive/2013/03/04/2942537.html 问题 项目要求struts2和servlet能够共存,就是stru ...

  7. linux网络编程 gethostbyname()

    gethostbyname()返回对应于给定主机名的包含主机名字和地址信息的hostent结构指针.结构的声明与gethostaddr()中一致. 返回对应于给定主机名的主机信息. #include ...

  8. 2084. Asm.Def的基本算法

    2084. Asm.Def的基本算法 传送门 ★☆   输入文件:asm_algo.in   输出文件:asm_algo.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] ...

  9. 数据结构与算法(3)----->队列和栈

    1. 栈和队列的基本性质 栈是先进后出;(像是子弹夹,后进先打出) 队列是先进先出;(像是平时排队买冰淇淋,按顺序轮流) 栈和队列在实现的结构上可以有数组和链表两种形式; (1)数组结构实现容易; ( ...

  10. 洛谷【P4883】mzf的考验

    浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...