an easy problem(贪心)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8333 | Accepted: 4986 |
Description
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".
Input
A line containing a number "0" terminates input, and this line need not be processed.
Output
Sample Input
1
2
3
4
78
0
Sample Output
2
4
5
8
83
Source
// Presentation Error(展示错误orz) mlgb没换行符报错 第一次出现这种错误整个人都蒙圈了
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int er[];
int main()
{
int n;
while(cin>>n&& n)//当能输出且不为零时
{
int k=,tot=;
memset(er,,sizeof(er));
while(n)//计算二进制
{
er[++k]=n%;
n/=;
}
k++;
for(int i=;i<=k;i++)
{
if(er[i]==)//找1的个数
{
tot++;
er[i]=;
if(er[i+]==)//从低位到高位找01
{
er[i+]=;//找到了就改为1; 相当于左移了
break;
}
}
}
for(int i=;i<=tot-;i++)
{
er[i]=;//将tot-1个1放在末尾,(右移),tot-1是因为其中一个左移了
}
int sum=;
for(int i=k;i>=;i--)//将二进制转换为十进制输出
{
sum=sum*+er[i];
}
printf("%d\n",sum);
}
return ;
}
an easy problem(贪心)的更多相关文章
- [openjudge] 1455:An Easy Problem 贪心
描述As we known, data stored in the computers is in binary form. The problem we discuss now is about t ...
- 一本通 1223:An Easy Problem
\[传送门qwq\] [题目描述] 给定一个正整数N,求最小的.比N大的正整数M,使得M与N的二进制表示中有相同数目的1. 举个例子,假如给定的N为78,其二进制表示为1001110,包含4个1,那么 ...
- 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 ...
- 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 ...
- 【暑假】[实用数据结构]UVa11991 Easy Problem from Rujia Liu?
UVa11991 Easy Problem from Rujia Liu? 思路: 构造数组data,使满足data[v][k]为第k个v的下标.因为不是每一个整数都会出现因此用到map,又因为每 ...
随机推荐
- hadoop06---多线程
.1.1. 实现线程的两种方式 1.继承Thread的方式 见代码MyThreadWithExtends 2.声明实现 Runnable 接口的方式 见代码MyThreadWithImpliment ...
- java Excel导入导出工具类
本文章,导入导出依赖提前定义好的模板 package com.shareworx.yjwy.utils; import java.io.File; import java.io.FileInputSt ...
- 建议42:使用pandas处理大型CSV文件
# -*- coding:utf-8 -*- ''' CSV 常用API 1)reader(csvfile[, dialect='excel'][, fmtparam]),主要用于CSV 文件的读取, ...
- 支持鼠标拖拽滑动的jQuery焦点图
在线演示 本地下载
- QGIS3.0.3+Qt5.9+VS2015_x64编译
QGIS3.0.3+Qt5.9+VS2015_x64编译 参考:https://blog.csdn.net/u010670734/article/details/80241615 https://ww ...
- CentOs linux安装SVN服务
SVN服务器有2种运行方式:1.独立服务器(例如:svn://xxx.com/xxx):2.借助apache (例如:http://svn.xxx.com/xxx):为了不依赖apache,我选择 ...
- Java中的UDP协议编程
一. UDP协议定义 UDP协议的全称是用户数据报,在网络中它与TCP协议一样用于处理数据包.在OSI模型中,在第四层——传输层,处于IP协议的上一层.UDP有不提供数据报分组.组装和不能对数据包 ...
- YARN作业提交流程剖析
YARN(MapReduce2) Yet Another Resource Negotiator / YARN Application Resource Negotiator对于节点数超出4000的大 ...
- hadoop mapreduce实现数据去重
实现原理分析: map函数数将输入的文本按照行读取, 并将Key--每一行的内容 输出 value--空. reduce 会自动统计所有的key,我们让reduce输出key-> ...
- IOS 拨打电话:4006 701 855 苹果中国
IOS 拨打电话:4006 701 855 苹果中国