Problem 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 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 ≤ 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.

Examples

Input

4500
47

Output

4747
47
解题思路:题目要求输出不小于n的最小整数,这个整数要求只含有4和7这两个数字,并且4出现的次数等于7出现的次数。暴力打表(大概2分钟),水过!
打表代码:
 #include<iostream>
using namespace std;
typedef long long LL;
bool judge(LL x){
int t1 = , t2 = ;
while (x){
int a = x % ;
if (a != && a != )return false;
if (a == )t1++;
if (a == )t2++;
x /= ;
}
if (t1 == t2)return true;
else return false;
}
int main(){
for (LL i = ; i < ; ++i)
if (judge(i)){ cout << i << ','; }
return ;
}
AC代码:
 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n,obj[]={,,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,};
int main(){
cin>>n;
for(int i=;;++i)
if(obj[i]>=n){cout<<obj[i]<<endl;break;}
return ;
}

再贴一种很好理解的递归写法(反正我没想到QAQ,值得学习一下)AC代码:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n,ans=1LL <<;
void f(LL x,int y,int z){//通过x构造答案,y是4的个数,z是7点个数
if(x>=n && y==z)ans=min(ans,x);//答案ans满足要求:1.大于等于n 2.只存在7和4,且7的个数等于4的个数
if(x>n*)return;//答案肯定在n和n*100之间,所以x>n*100的时候退出;
f(x*+,y+,z);//当前答案x加一位数4
f(x*+,y,z+);//当前答案x加一位数7
}
int main(){
cin>>n;
f(,,);
cout<<ans<<endl;
return ;
}

C - Lucky Numbers (easy)的更多相关文章

  1. ZCMU 2177 Lucky Numbers (easy)

    传送门: http://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=2177 2177: Lucky Numbers (easy) 时间限制: 2 Sec   ...

  2. 题解 CF1428G Lucky Numbers (Easy Version and Hard Version)

    这题没有压行就成 \(\texttt{Hard Version}\) 最短代码解了( 要知道这题那么 \(sb\) 就不啃 \(D\) 和 \(E\) 了. \(\texttt{Solution}\) ...

  3. HDU 5676 ztr loves lucky numbers (模拟)

    ztr loves lucky numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/I Description ztr ...

  4. codeforces 630C Lucky Numbers

    C. Lucky Numbers time limit per test 0.5 seconds memory limit per test 64 megabytes input standard i ...

  5. hdu 5676 ztr loves lucky numbers(dfs+离线)

    Problem Description ztr loves lucky numbers. Everybody knows that positive integers are lucky if the ...

  6. codeforces 630C - Lucky Numbers 递推思路

    630C - Lucky Numbers 题目大意: 给定数字位数,且这个数字只能由7和8组成,问有多少种组合的可能性 思路: 假设为1位,只有7和8:两位的时候,除了77,78,87,88之外还哇哦 ...

  7. hdu 5676 ztr loves lucky numbers 打表+二分

    ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  8. hdu-5676 ztr loves lucky numbers(乱搞题)

    题目链接: ztr loves lucky numbers  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65536 K ( ...

  9. Codeforces Round #160 (Div. 2)---A. Roma and Lucky Numbers

    Roma and Lucky Numbers time limit per test 1 second memory limit per test 256 megabytes input standa ...

随机推荐

  1. Django - 视图获取请求头

    1.urls.py(url和函数对应关系) 2.通过request.evniron,返回request的所有信息,用索引的方式,获取用户请求头信息. 3.也可以通过key,value方式,来展示请求头 ...

  2. 第六节:pandas函数应用

    1.pipe() :表格函数应用: 2.apply():表格行列函数应用: 3.applymap():表格元素应用.

  3. HDU 3208 Integer’s Power

    Integer’s Power Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Origina ...

  4. [POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]

    可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include &l ...

  5. 无管理员帐号的WIN7,如果使用自己的JDK版本?

    因为公司的电脑只有普通权限, 而且JDK版本低了. 那我只好用BAT脚本来导入自己的环境啦,毕竟每次在CMD窗口输入太繁琐. set JAVA_HOME=D:\JDK8 set CLASSPATH=D ...

  6. 络谷 P2865 [USACO06NOV]路障Roadblocks

    P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...

  7. CF #321 (Div. 2) D

    不说了,爆内存好几次,后来醒起状态有重复... 状压+TSP #include <iostream> #include <cstdio> #include <cstrin ...

  8. c语言char 和int的问题

    参考:http://www.cnblogs.com/dire/p/5222968.html 参考baidu: char和int的定义我是清楚的,现在有一个问题: 1.设A和B是int型,C是char型 ...

  9. eclipse中Client/Server程序生成exe

    先建两个Java Project项目,一个写Client,一个写Server端程序,程序大致为一个Server端建立监听某个port.多个Client端能够连接,实现例如以下: 1.      Ser ...

  10. mysql学习之四:sql语句学习2

    创建数据库: CREATE DATABASE stefan; 删除数据库: DROP DATABASE stefan; 重命名数据库: 重命名数据库没有直接的办法. 已经不再使用的方法: RENAME ...