描述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. css浏览器兼容问题集锦

    表单按钮用input type=submit和a链接两者表现不一致的问题 表单的输入框.文本.验证码图片没有对齐 IE6/7中margin失效 IE6中margin双边距 1.问题: 表单按钮用inp ...

  2. NET LOCALGROUP命令详解(将用户添加到管理员组等)

    NET LOCALGROUP [groupname [/COMMENT:"text"]] [/DOMAIN] groupname {/ADD [/COMMENT:"tex ...

  3. Contiki 2.7 Makefile 文件(一)

    一.主控Makefile 这里以hello-world例子为主线,从其工程Makefile开始,解析整个build过程.

  4. 关于MVC模板渲染的一点小事type="text/template"

    先上一个demo,简单粗暴,请自便 <!DOCTYPE html> <html> <head lang="en"> <meta chars ...

  5. Linux下安装二进制版mysql-8.0.15

    1.添加用户## 添加用户组groupadd mysql## 添加用户,指定用户home目录useradd -g mysql mysql -d /data/mysql## 解压下载的mysql二进制包 ...

  6. BZOJ-3940:Censoring(AC自动机裸题)

    Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have p ...

  7. 【C++基础】浅拷贝和深拷贝

    简单理解: 对于一块内存,浅拷贝只是增加了一个指针,这样两个变量都指向这块内存,二深拷贝则是先开辟一块同等大小的新内存区,将待拷贝内存的内容复制过来,再赋予一个指向新内存的指针.区别在于:浅拷贝会造成 ...

  8. TCP头部格式详解,附Wireshark对TCP头部抓包分析

    TCP之所以能为数据通讯提供可靠的传输,主要在于TCP数据包头部功能非常多. 那么,我们先来看看TCP头部格式(RFC 793.1323定义了TCP头部): TCP头部格式中的内容解析如下:(文末还有 ...

  9. Oracle字段增删改方法总结

    一.修改字段的语法:alter table tablename modify (字段名 类型 [default value][null/not null],….);有一个表名为tb,字段段名为name ...

  10. 用Pyinstaller把Python3.7程序打包成可执行文件exe

    1.通过pip3 install pyinstaller 安装成功 2.然后执行命令,首先:需要切换到程序所在的目录 执行命令 pyinstaller -F -w <文件名.py>,-F代 ...