[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 ...
随机推荐
- pymysql 模块的使用
一 . pymysql 的下载和使用 在python 中操作数据库需要用到 pymysql 模块. (1) . pymysql 模块的下载 pip3 install pymysql (2) . ...
- Spring Boot2.0之 jar打包方式
Jar类型打包方式 1.使用mvn celan package 打包 2.使用java –jar 包名 war类型打包方式 1.使用mvn celan package 打包 2.使用java –ja ...
- Adding Form Fields to a MS Word Document
Configuring a Word Merge in SmartSimple is a three-step process: Create the MS Word document that wi ...
- &&、||和&、|的区别
1. && .|| 和 &.| 都是逻辑运算符,前两个 与后两个的区别就在于 &&.|| 有"短路"现象,而& .| 则没有. 例如 ...
- Listen 82
Doc Calls Deconditioning a Condition Doctors know a lot about prescribing medications. "Take tw ...
- C语言的内存四区模型和函数调用模型
首先是操作系统将代码程序加载到内存中 然后将内存分为4个区 栈区,程序的局部变量区,函数传递的参数,由编译器自动进行内存资源的释放. 堆区,动态内存申请,如果不手动释放内存,则这块内存不会进行析构. ...
- HTml js 生成图片
<script type="text/javascript"> function $(id) { return document.getElementById(id); ...
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- 变废为宝,用旧电脑自己DIY组建 NAS 服务器
i17986 出品,必属佳作! 前言: 老外不喜欢升级硬件和软件,大家应该都知道.我昨天无意看到 FreeNAS 自述文件,这个系统可以让你使用旧的计算机硬件,于是我决定这么做.垃圾电脑你怎么能没有, ...
- Restore Points 制定回退方案
Restore Points 制定回退方案 背景:Flashback Database 和 restore points 都可以提供一个基于时间点的回滚. 理论:1) Normal Restore P ...