C - Lucky Numbers (easy)
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)的更多相关文章
- ZCMU 2177 Lucky Numbers (easy)
传送门: http://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=2177 2177: Lucky Numbers (easy) 时间限制: 2 Sec ...
- 题解 CF1428G Lucky Numbers (Easy Version and Hard Version)
这题没有压行就成 \(\texttt{Hard Version}\) 最短代码解了( 要知道这题那么 \(sb\) 就不啃 \(D\) 和 \(E\) 了. \(\texttt{Solution}\) ...
- HDU 5676 ztr loves lucky numbers (模拟)
ztr loves lucky numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/I Description ztr ...
- codeforces 630C Lucky Numbers
C. Lucky Numbers time limit per test 0.5 seconds memory limit per test 64 megabytes input standard i ...
- hdu 5676 ztr loves lucky numbers(dfs+离线)
Problem Description ztr loves lucky numbers. Everybody knows that positive integers are lucky if the ...
- codeforces 630C - Lucky Numbers 递推思路
630C - Lucky Numbers 题目大意: 给定数字位数,且这个数字只能由7和8组成,问有多少种组合的可能性 思路: 假设为1位,只有7和8:两位的时候,除了77,78,87,88之外还哇哦 ...
- hdu 5676 ztr loves lucky numbers 打表+二分
ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- hdu-5676 ztr loves lucky numbers(乱搞题)
题目链接: ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- 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 ...
随机推荐
- R 安装car包失败
在RStudio里安装car包的时候报错 /usr/bin/ld: cannot find -llapack /usr/bin/ld: cannot find -lblas make: *** [qu ...
- 【codeforces 797E】Array Queries
[题目链接]:http://codeforces.com/problemset/problem/797/E [题意] 给你一个n个元素的数组; 每个元素都在1..n之间; 然后给你q个询问; 每个询问 ...
- [POJ1226]Substrings(后缀数组)
传送门 给定 n 个字符串,求出现或反转后出现在每个字符串中的最长子串. 算法分析: 这题不同的地方在于要判断是否在反转后的字符串中出现.其实这并没有加大题目的难度. 只需要先将每个字符串都反过来写一 ...
- 清北学堂模拟赛d4t1 a
分析:大模拟,没什么好说的.我在考场上犯了一个超级低级的错误:while (scanf("%s",s + 1)),导致了死循环,血的教训啊,以后要记住了. /* 1.没有发生改变, ...
- POJ - 3541 - Given a string…
Given a string… Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 1819 Accepted: 390 C ...
- CODEVS——T 1049 棋盘染色
http://codevs.cn/problem/1049/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descr ...
- php生成随机password的几种方法
文章来源:PHP开发学习门户 地址:http://www.phpthinking.com/archives/523 使用PHP开发应用程序,尤其是站点程序.经常须要生成随机password,如用户注冊 ...
- pytest 失败用例重试
https://www.cnblogs.com/jinzhuduoduo/articles/7017405.html http://www.lxway.com/445949491.htm https: ...
- java的死锁学习
学习java的死锁写的代码 也是看书上的然后自己敲了一个 <span style="font-size:18px;">package synchronization.j ...
- linux下多线程的调试
多线程调试的基本命令(均在gdb命令行使用): info threads ---- 显示当前可调试的全部线程.每个线程都有自己的线程ID,显示结果中前面有*的表示当前调试的线程. eg: ...