B. Lucky Numbers
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. For example, numbers 47, 7744, 474477 are super lucky and 4, 744, 467 are not.

One day Petya came across a positive integer n. Help him to find the least super lucky number which is not less than n.

Input

The only line contains a positive integer n (1 ≤ n ≤ 10^100000). This number doesn't have leading zeroes.

Output

Output the least super lucky number that is more than or equal to n.

Examples
input

output

input

output

题意:给你一个数字(位数<=10^100000)求不小于他且只由4,7并且4跟7的个数相等的最小的数字

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a));
char s[100000+10],s2[100000+10]; int main()
{
string s;
while(cin>>s)
{
int l=s.size();
if(l%2!=0||s>string(l/2,'7')+string(l/2,'4'))
{cout<<string(l/2+1,'4')<<string(l/2+1,'7')<<"\n";continue;}
int n4=l/2,n7=l/2; int flag=0;
string ans;
for(int i=0;i<s.size();i++)
{
int ok=0;
if(n4>0)
{
if(flag||s[i]<'4') ok=1;
else if(s[i]=='4') for(int j=i+1;j<=l;j++)
{
if(j==l) {ok=1;break;}
char ch=j-i<=n7?'7':'4';
if(ch>s[j]) ok=1;
if(ch!=s[j]) break;
}
}
if(ok) {ans+="4";n4--;}
else {ans+="7";n7--;}
if(ans[i]>s[i]) flag=1;
}
cout<<ans<<"\n";
}
return 0;
}

  分析:比赛时主要是比如4500<7744这样需要构造的数字没想出来。其实还是比较简单的,

因为要让数字足够小,所以从左往右贪心的尽可能的用4,如果当前数字<4,那么肯定就用4了,但是如果等于4呢,那就无法通过当前位判断出来了,需要假设当前位放4,那么为了>=数据4500,接下来的一个位数需要尽可能大,那么就用7,比如47,当到达第三位的时候7>0,说明当前位放4虽然跟数据的数值一样,但是是可以通过后面的数字构造出一个比数据大的,所以当前位放4,否则放7

注意string放元素是a+="w";

最新文章

  1. window.hostory(浏览器的历史记录)
  2. CSS3实现鼠标移动到图片上图片变大
  3. fastcoloredtextbox 中文不重叠
  4. vue.js插件使用(01) vue-resource
  5. Python线性时间排序——桶排序、基数排序与计数排序
  6. 使用AccessibilityService模拟点击事件失败的分析
  7. 解决weblogic与系统时间相差8小时的问题
  8. 我家用的网络IP地址给定,MAC绑定,我买了个无线路由器,请问怎么设定能让我的电脑和手机都能上网
  9. LDAP禁止匿名访问
  10. jQuery 学习小结
  11. Python基础(set集合)
  12. 分别用Excel和python进行日期格式转换成时间戳格式
  13. CentOS7.5实践快速部署LAMP+Tomcat成功运行阿里云或者腾讯云
  14. mysql服务自启【Linux】
  15. Java大数练习
  16. 使用DotfuscatorPro_4.9对软件dll库进行加密
  17. PostgreSQL 使用小点
  18. EntityFrameworkCore操作记录
  19. python使用(二)
  20. 【css】css 背景色渐变兼容写法

热门文章

  1. layer插件loading快速应用示例
  2. JS基础_嵌套的for循环
  3. easyui-datagrid 编辑模式详解——combobox
  4. 记录一则ORA
  5. Odoo的菜单项
  6. pam_smb
  7. 4.Nginx配置文件Nginx.conf_虚拟主机配置规则
  8. 很有用的shell脚本
  9. API开发之接口安全(一)----生成sign
  10. 生产者消费者问题--BlockingQueue