zoukankan      html  css  js  c++  java
  • CodeForces

     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("
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    Redis 配置连接池,redisTemplate 操作多个db数据库,切换多个db,解决JedisConnectionFactory的设置连接方法过时问题。(转)
    Spring Boot 中初始化资源的几种方式(转)
    关于RedisTemplate和StringRedisTemplate(转)
    @PostConstruct
    Python % 格式化字符串
    逻辑回归模型
    python 常用内置函数
    HIVE: collect_set(输出未包含在groupby的字段);
    HDFS文件和HIVE表的一些操作
    Linux 传输文件
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770951.html
Copyright © 2011-2022 走看看