[openjudge] 1455:An Easy Problem 贪心
描述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 贪心的更多相关文章
- an easy problem(贪心)
An Easy Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8333 Accepted: 4986 D ...
- 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 ...
- 1455:An Easy Problem
传送门:http://noi.openjudge.cn/ch0406/1455/ /-24作业 //#include "stdafx.h" #include<bits/std ...
- 一本通 1223:An Easy Problem
\[传送门qwq\] [题目描述] 给定一个正整数N,求最小的.比N大的正整数M,使得M与N的二进制表示中有相同数目的1. 举个例子,假如给定的N为78,其二进制表示为1001110,包含4个1,那么 ...
- UVA-11991 Easy Problem from Rujia Liu?
Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...
- An easy problem
An easy problem Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- 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, ...
- POJ 2826 An Easy Problem?!
An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7837 Accepted: 1145 ...
- hdu 5475 An easy problem(暴力 || 线段树区间单点更新)
http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...
随机推荐
- 认识影片版本(CAM、TS、TC、DVD、HD、BD、TVRIP等)
许多朋友在下载电影的时候, 往往会被各种各样的版本标识弄糊涂,今天把各种版本的缩写收集在一起,希望对大家有所帮助 . 引用: 1.CAM(枪版) CAM 通常是用数码摄像机从电影院盗录.有时会使 ...
- python基础教程_学习笔记11:魔法方法、属性和迭代器
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/signjing/article/details/31417309 魔法方法.属性和迭代器 在pyth ...
- PHP使用Apache中的ab测试网站的压力性能
打开Apache服务器的安装路径(我用的是 WampServer),在bin目录中有一个ab.exe的可执行程序,它就是要介绍的压力测试工具. 在Windows系统的命令行下,进入ab.exe程序所在 ...
- JETSON TK1 ~ 控制GPIO
首先建立个存放gpio代码的文件夹,CD到该文件夹. git clone git://github.com/derekmolloy/boneDeviceTree/ 解压后会出现几个文件 GPIO文件夹 ...
- Java基础教程:对象比较排序
Java基础教程:对象比较排序 转载请标明出处:http://blog.csdn.net/wangtaocsdn/article/details/71500500 有时候需要对对象列表或数组进行排序, ...
- openocd+jlink为mini2440调试u-boot
需要安装openocd,如果已经安装了系统默认的openocd(默认是0.5.0,版本太低),需要先卸载掉. 在安装前需要安装必需的一些库文件: -dev libusb-1.0-0 automake ...
- 51Nod 1627 瞬间移动 —— 组合数学
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1627 1627 瞬间移动 基准时间限制:1 秒 空间限制:1 ...
- UVA12103 —— Leonardo's Notebook —— 置换分解
题目链接:https://vjudge.net/problem/UVA-12103 题意: 给出大写字母“ABCD……Z”的一个置换B,问是否存在一个置换A,使得A^2 = B. 题解: 对于置换,有 ...
- Spark- ERROR Shell: Failed to locate the winutils binary in the hadoop binary path java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.
运行 mport org.apache.log4j.{Level, Logger} import org.apache.spark.rdd.RDD import org.apache.spark.{S ...
- 关于for 循环里 线程执行顺序问题
最近在做项目时遇到了 这样的需求 要在一个for循环里执行下载的操作, 而且要等 下载完每个 再去接着走循环.上网查了一些 觉得说的不是很明确.现在把我用到的代码 贴上 希望可以帮到有此需求的开发者 ...