描述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. 1
  2. 2
  3. 3
  4. 4
  5. 78
  6. 0

样例输出

  1. 2
  2. 4
  3. 5
  4. 8
  5. 83
  6.  
  7. 普通的暴力可以过 比较慢就是了
    1的个数不变,那就找交换规律,其实只要从后向前找01子串交换,相当于进位了,再将后面的1依次放到末尾就行了
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cstring>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int solve(int n)
  8. {
  9. int b[], ans = ;
  10. memset(b, , sizeof(b));
  11. int k = ;
  12. while (n) {
  13. b[k++] = n % ;
  14. n /= ;
  15. }
  16. k++;
  17.  
  18. int cnt = ;
  19. for (int i = ; i < k; i++) {
  20. if (b[i] && b[i+]) {
  21. cnt++;
  22. b[i] = ;
  23. }
  24. if (b[i] && !b[i+]) {
  25. b[i] = ;
  26. b[i+] = ;
  27. break;
  28. }
  29. }
  30. for (int j = ; j < cnt; j++)
  31. b[j] = ;
  32.  
  33. for (int i = k; i >= ; i--) {
  34. ans = ans* + b[i];
  35. }
  36. return ans;
  37. }
  38.  
  39. int main()
  40. {
  41. //freopen("1.txt", "r", stdin);
  42.  
  43. int n;
  44. while (cin >> n && n) {
  45. cout << solve(n) << endl;
  46. }
  47.  
  48. return ;
  49. }
  1.  

[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. Linux安装mariadb详细步骤

    1.安装mariadb yum和源码编译安装的区别? 1.路径区别-yum安装的软件是他自定义的,源码安装的软件./configure --preifx=软件安装的绝对路径 2.yum仓库的软件,版本 ...

  2. ExtJS教程(5)---Ext.data.Model之高级应用

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jaune161/article/details/37391399 1.Model的数据验证 这里借助 ...

  3. linux-shell脚本命令之grep

    版权声明: https://blog.csdn.net/zdp072/article/details/26015611 [ grep简单介绍: ] grep是用来过滤含有特定字符的行, 能使用正則表達 ...

  4. 各种python 函数參数定义和解析

    python 中的函数參数是赋值式的传递的,函数的使用中要注意两个方面:1.函数參数的定义过程,2.函数參数在调用过程中是怎样解析的. 首先说一下在python 中的函数调用过程是分四种方式的.这里且 ...

  5. mysql错误:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated

    今天迁移django数据库的时候,跑程序的时候出现这样的错误: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY cla ...

  6. uploadify 报错 超过了最大请求长度

    今天系统遇到了一个问题,上传4m以上的文件,uploadify就会报错:超过了最大请求长度. 开始我以为是设置的大小,可是后来我看了uploadify的fileSizeLimit=1024*10,也就 ...

  7. Dubbo之生产者

    环境步骤: 安装Zookeepr启动 创建Maven项目搭建生产者和消费者 安装DubboAdmin平台,实现监控 Dubbo注册中心采用的是Zookeeper.为什么采用Zookeeper呢? Zo ...

  8. Kbuntu16.04利用快捷键调用终端Konsole

    之前用其他linux,可以按ctrl alt t三个键快速调用终端.但是我用Kbuntu16.04这个版本的时候却不行.于是跑去自定义了一下下. System Settings  -->  Wo ...

  9. static语句块的执行时间

    package utfTest; public class Test01 { public static void main(String[] args) { //Person.show(); Sys ...

  10. rand()与srand()

    1.不用srand()的话 两次运行程序产生的随机数序列相同 2.用srand() 两次运行程序产生的随机数则不同 示例程序: #include<iostream> #include< ...