CodeForces
来源:互联网 发布:java装饰模式和注解 编辑:程序博客网 时间:2024/05/20 21:23
Petya喜欢幸运数字。 大家都知道,正数整数是幸运的,如果它们的十进制表示不包含4和7以外的数字。例如,数字47,744,4是幸运的,5,17,46,7不是。 幸运数字超级幸运,如果它的十进制表示包含相等数量的数字4和7.例如,数字47,7744,474477是超级幸运,4,744,467不是。 有一天,Petya遇到了一个正整数n。 帮助他找出大于等于给定的数字的最小的超级幸运数字.
Input
输入一个正整数n(n<1e9)。
Output
输出大于等于给定的数字的最小的超级幸运数字.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.
Example
Input
4500Output4747Input47Output47
这道题用dfs来做,dfs时先让末尾加上4,继续搜索,找不到把4变成7(cnt1不变即可).直到找到符合条件的数字直接退出即可,由于搜索的顺序性,找到的第一个数就是答案.
代码:
#include<bits/stdc++.h>#define ll long longusing namespace std;int pp=0,n;void dfs(int cnt1,int cnt2,int len,ll number){ if(cnt1==len&&cnt2==len) { if(number>=n) {cout<<number<<endl;pp=1;exit(0);} return; } if(cnt1<len) dfs(cnt1+1,cnt2,len,number*10+4); if(cnt2<len) dfs(cnt1,cnt2+1,len,number*10+7);}int main(){ cin>>n; pp=0; for(int i=1;i<=9;i++) { if(pp==1) break; dfs(0,0,i,0); } return 0;}
阅读全文
0 0
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- BBP算法板子
- 2016 China-Final Ice Cream Tower(二分)
- sql
- 【AI_数学知识】概率论
- Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks论文笔记
- CodeForces
- iOS去除导航栏和tabbar的1px横线
- 不同品牌的光模块可以互连吗?
- C++11新特性
- Android移动开发-Android屏幕手写签名的实现
- ThreadLocal理解-结合连接池
- spring-bean的理解
- POJ 1002(不用排序的方法)(简单的字符串处理程序)
- Attention Model(mechanism) 的 套路