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. 安装 jdk 和 IDE软件

    1.jdk的安装 通过官方网站获取JDK http://www.oracle.com 针对不同操作系统,下载不同的JDK版本 识别计算机的操作系统 下载完后进行安装,傻瓜式安装,下一步下一步即可.用j ...

  2. Going Home HDU - 1533(最大费用最小流)

    On a grid map there are n little men and n houses. In each unit time, every little man can move one ...

  3. UID中RUID、EUID和SUID的区别

    看UNIX相关的书时经常能遇到这几个概念,但一直没有好好去理清这几个概念,以致对这几个概念一直一知半解.今天好好区分了一下这几个概念并总结如下.说白了这几个UID引出都是为了系统的权限管理. 下面分别 ...

  4. Python 爬虫之第一次接触

    爬豆瓣网电影TOP250名单 ------- 代码未写完,等待更新 import requests from requests.exceptions import RequestException i ...

  5. 3 numpy模块

    Numpy     什么是Numpy:Numeric Python         Numpy模块是Python的一种开源的数值计算扩展.             1 一个强大的N维数组对象Array ...

  6. Codeforces Round #400 (Div. 1 + Div. 2, combined)——ABCDE

    题目戳这里 A.A Serial Killer 题目描述似乎很恶心,结合样例和样例解释猜测的题意 使用C++11的auto可以来一手骚操作 #include <bits/stdc++.h> ...

  7. hibernate分表保存日志

    @Service("accessLogService")@Transactionalpublic class LogMessageServiceImpl extends BaseD ...

  8. Delphi春天将来临,Android遇到XE7我也是醉了,Hello World

    回首往日,从Delphi 7走到如今.总感觉不愠不火.期间论坛倒掉无数,没倒掉的也半死不活,大批的程序猿转向C#,Java,PHP. Delphi的开发高效有目共睹,一直不忍放弃.Delphi以前一夜 ...

  9. QT中使用高速排序

    今天想到了用QT做一个高速排序.所以研究了一下. 由于用习惯了,C++的std::sort.就算是C的时候也用得是stdlib.h中的qsort. 手写板 手写板的快排事实上不难,仅仅是自从用C++打 ...

  10. 【JavaScript】离开页面前提示

    离开页面前的提示不能够用onunload去做,由于它仅仅是兼容IE,你要兼容Google与FireFox就蛋疼了. 并且这个事件还是关闭之后才会触发的. 取而代之能够用onbeforeunload去实 ...