描述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进阶之旅------>Android通过使用Matrix旋转图片来模拟碟片加载过程

    今天实现了一个模拟碟片加载过程的小demo,在此展示一下.由于在公司,不好截取动态图片,因此就在这截取两张静态图片看看效果先. 下面简单的将代码列出来. setp1.准备两张用于旋转的图片,如下:lo ...

  2. python 的print 用法

    print(x,y) 等价于 import sys sys.stdout.write(str(x) + ' ' +str(y) + '\n') 从语法上讲,调用python3.0 的print 函数有 ...

  3. codeforces 之 Little Pigs and Wolves

    B-Little Pigs and Wolvestime limit per test2 secondsmemory limit per test256 megabytesinputstandard ...

  4. 深入理解JVM - 线程安全与锁优化 - 第十三章

    线程安全 当多个线程访问一个对象时,如果不用考虑这些线程在运行时环境下的调度和交替执行,也不需要进行额外的同步,或者在调用方法进行任何其他的协调操作,调用这个对象的行为都可以获得正确的结果,那么这个对 ...

  5. layer 插件 在子页面关闭自身的方法

    先取到该子页面在父级页面中的name 值 var  index= parent.layer.getFrameIndex(Window.name); 然后用该方法关闭 parent.layer.clos ...

  6. openfire插件开发环境

    创建java工程 SamplePlugin: package com.hoo.server.plugin; import java.io.File; import org.jivesoftware.o ...

  7. oracle数据库复习(1)

    数据库中的专业术语: 表:在数据库中存放数据所用的表 视图:数据库中的虚拟表.在视图中存放的是从数据表中查询出来的纪录 存储过程:存储过程是由SQL语句和控制流语句组成的代码块.存储过程在开发软件时, ...

  8. STL中mem_fun和mem_fun_ref的用法

    例如:假设有如下的代码: class Employee { public: int DoSomething(){} } std::vector<Employee> Emps; 假设我们要调 ...

  9. BZOJ-4003:城池攻占(可并堆+lazy标记)

    小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池. 这 n 个城池用 到 n 的整数表示.除 号城池外,城池 i 会受到另一座城池 fi 的管辖, 其中 fi <i.也就是 ...

  10. ACM学习历程—BestCoder 2015百度之星资格赛1004 放盘子(策略 && 计算几何)

    Problem Description 小度熊喜欢恶作剧.今天他向来访者们提出一个恶俗的游戏.他和来访者们轮流往一个正多边形内放盘子.最后放盘子的是获胜者,会赢得失败者的一个吻.玩了两次以后,小度熊发 ...