CodeForces - 96B Lucky Numbers (easy)

 Lucky Numbers (easy)
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 477444 are lucky and 517467 are not.

Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. For example, numbers 477744,474477 are super lucky and 4744467 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 ≤ 109). This number doesn't have leading zeroes.

Output

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

Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.

Sample Input

Input
4500
Output
4747
Input
47
Output
47

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX(a, b)       (a > b)? a: b
#define MIN(a, b)       (a < b)? a: b
#define  MAX_N 105
using namespace std;

char str[MAX_N];
int use[MAX_N];
int c4, c7, l;

bool change(int x) {
	if (x == 0)	return true;
	if (str[x - 1] == '4')	{
		str[x - 1] = '7';
		l = x - 1;
		return false;
	}
	else return change(x - 1);
}

int main() {
	//freopen("e:\\ACM\\duipai\\data.txt", "r", stdin);
	//freopen("e:\\ACM\\duipai\\out1.txt", "w", stdout);
	while (cin>>str) {
		//flag标记是否可以有与输入数位数相同的super lucky,vis 标记是否最小的数字选择大于其的
		bool flag = false, vis = false;
		int len = strlen(str);
		//奇位数直接跳过
		if (len % 2 == 1)	flag = true;
		if (!flag) {
			//用c4,c7表示47各自剩余的数
			c4 = len / 2, c7 = len / 2;
			for (int i = 0; i < len; i++) {
				//小于等于4,或者被标记,且还有4可以用,进入
				if ((str[i] <= '4' || vis) && c4)	{
					//如果最低选择位小于4,标记
					if (str[i] < '4')	vis = true;
					str[i] = '4';
					c4--;
				}
				
				
				
				//7有剩余,被标记过或者位于4—7之间。
				else if (c7 && (vis || str[i] <= '7')) {
					if (str[i] < '7')	vis = true;
					str[i] = '7';
					c7--;
				}
				else {
					//向上查找,有4换为7,否则跳出。
					if(flag = change(i)) break;
					else {
						i = l;
						//从新取c4,c7的值应用
						c4 = use[i] / 100 + 1;
						c7 = use[i] % 100 - 1;
						vis = true;
					}
				}
				//用use记录各个状态的c4,与c7的值,方便再次调用
				use[i] = c4 * 100 + c7;
				if (c7 == -1)	{
					flag = true;
					break;
				}
			}
		}
		if (flag) {
			if (len % 2 == 0)	printf("4");
			for (int i = 0; i < (len + 1) / 2; i++) {
				printf("4");
			}
			for (int i = 0; i < (len + 1) / 2; i++) {
				printf("7");
			}
			if (len % 2 == 0)   printf("7");
		}
		else {
			for (int i = 0; i < len; i++) {
				printf("%c", str[i]);
			}
		}
		printf("\n");
	}
	return 0;
}


转载于:https://www.cnblogs.com/cniwoq/p/6770951.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值