题目链接;

http://acm.hdu.edu.cn/showproblem.php?pid=5676

题意:

由4和7组成的且4和7出现次数相同的数称为幸运数字,给定n,求不大于n的最大幸运数字。

分析:

可以对于每个数都按位dfs找一发。一旦发现当前位无法满足就回溯,直到找到满足条件的最小的。

也可以先按位dfs把所有结果都找出来存起来,然后对于每个询问直接二分即可。注意边界时会爆long long,注意处理。

代码:

#include<cstdio>
#include<cstring>
char s[50];
int len;
bool found;
int four, seven;
void write(int len)
{
for(int i = 0; i < len/2; i++) printf("4");
for(int i = 0; i < len/2; i++) printf("7");
printf("\n");
}
void dfs(long long ans, long long res, int lens)
{
if(lens == len && !found){
found = true;
printf("%I64d\n", ans);
}
if(found) return;
res = res * 10 + s[lens] - '0';
if(res <= ans * 10 + 4 && four < len / 2){
four++;
dfs(ans * 10 + 4, res, lens + 1);
four--;
res /= 10;
}
if(found) return;
if(res <= ans * 10 + 7 && seven < len / 2){
seven++;
dfs(ans * 10 + 7, res, lens + 1);
seven--;
res /= 10;
}
}
int main()
{
int T;scanf("%d", &T);
while(T--){
scanf("%s", s);
len = strlen(s);
if(len & 1) write(len + 1);
else{
four = seven = 0;
found = false;
dfs(0, 0, 0);
if(!found) write(len + 2);
}
}
return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
long long a[100000 + 5];
int tot = 0;
void dfs(int four, int seven, long long ans)
{
if(four == seven) a[tot++] = ans;
if(four < 9) dfs(four + 1, seven, ans * 10 + 4);
if(seven < 9) dfs(four, seven + 1, ans * 10 + 7);
}
int main (void)
{
dfs(0, 0, 0);
int t;cin>>t;
sort(a, a + tot);
while(t--){
long long n;cin>>n;
if(!n) {cout<<47<<endl;continue;}
int res = lower_bound(a, a + tot, n) - a;
if(res == tot) cout<<"44444444447777777777"<<endl;
else cout<<a[res]<<endl;
}
return 0;
}

HDU 5676 ztr loves lucky numbers【DFS】的更多相关文章

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

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

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

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

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

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

  4. hdu 5676 ztr loves lucky numbers

    题目链接:hdu 5676 一开始看题还以为和数位dp相关的,后来才发现是搜索题,我手算了下,所有的super lucky number(也就是只含数字4, 7且4, 7的数量相等的数)加起来也不过几 ...

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

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

  6. hdu5676 ztr loves lucky numbers(dfs)

    链接 ztrloveslucky numbers 题意 定义幸运数为:只存在4和7且4和7数量相等的数,给出n,求比>=n的最小幸运数 做法 暴力搜出所有长度从2-18的幸运数,因为最多9个4, ...

  7. ztr loves lucky numbers 傻逼的我来了个大模拟

    http://acm.hdu.edu.cn/showproblem.php?pid=5676 这题的正解因该是dfs的,但是有18个位,然后我一算,全排列的话,有18!个啊,那不是很大?但是有很多是相 ...

  8. Codeforces 1036C Classy Numbers 【DFS】

    <题目链接> 题目大意: 对于那些各个位数上的非0数小于等于3的数,我们称为 classy number ,现在给你一个闭区间 [L,R]  (1≤L≤R≤1018).,问你这个区间内有多 ...

  9. hdu 1518 Square 木棍建正方形【DFS】

    题目链接 题目大意: 题意就是输入棍子的数量和每根棍子的长度,看能不能拼成正方形. #include <bits/stdc++.h> using namespace std; int n, ...

随机推荐

  1. CPP-基础:strcpy之于C++(

    以下对strcpy函数错误的是? char atr1[]="string"; ]; char *str3; char *str4="sting"; A.strc ...

  2. CPP-练习

    HW: 1.局部变量能否和全局变量重名? 答:能,局部会屏蔽全局.要用全局变量,需要使用"::" ;局部变量可以与全局变量同名,在函数内引用这个变量时,会用到同名的局部变量,而不会 ...

  3. JavaScript中的显示原型和隐形原型(理解原型链)

    显式原型:prototype 隐式原型:__proto__ 1.显式原型和隐式原型是什么? 在js中万物皆对象,方法(Function)是对象,方法的原型(Function.prototype)是对象 ...

  4. StringMVCWeb接受前台值的几种方式

    这些决定与request   header   的Content-Type属性 1.通过@RequestParam @RequestParam Map<String, Object> pa ...

  5. 洛谷五月月赛【LGR-047】划水记

    虽然月赛有些爆炸,但我永远资瓷洛谷! 因为去接水,所以迟到了十几分钟,然后洛谷首页就打不开了-- 通过洛谷题库间接打开了比赛,看了看\(TA\),WTF?博弈论?再仔细读了读题,嗯,判断奇偶性,不过要 ...

  6. Bootstrap历练实例:表单帮助文件

    Bootstrap表单控件可以在输入框input上有一个块级帮助文本,为了添加一个占用整个宽度的内容块,请在input后添加help-block. 实例: <!DOCTYPE html>& ...

  7. shell脚本,awk 根据文件某列去重并且统计该列频次。

    a文件为 a a a s s d .怎么把a文件变为 a s d .怎么把a文件变为 a a a s s d 解题方法如下: 解题思路 [root@localhost study]# awk 'NR= ...

  8. ASP.NET 开发人员不必担心 Node 的五大理由

    哦别误会……我真的很喜欢 Node,而且我觉得它提出的概念和模式将在很长一段时间内,对服务端 Web 编程产生深远的影响.即使随着时间的推移 Node 过气了,我们肯定可以从下一个牛逼玩意身上或多或少 ...

  9. GIMP模板选区操作

    选择方法有很多种,这里我就新学的方法记录一下,主要是通过小剪刀和Toggle Quick Mask 相结合的运用. 选择Scissors Select Tool工具 设置基本的属性:Antialisa ...

  10. service worker 消息推送

    https://developers.google.com/web/fundamentals/codelabs/push-notifications/?hl=en 首先下载源码: git clone ...