题目链接;

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. win7上安装虚拟机并上网

    一.安装Workstation Pro 二.下载CentOS-7-x86_64-DVD-1511.iso包 三.创建新的虚拟机,按照向导安装即可 四.使用cd /etc/sysconfig/netwo ...

  2. CPP-网络/通信:POST

    BOOL PostSubmit(CString strUrl,const CString&strPara, CString&strContent){ BOOL bRet=FALSE; ...

  3. 洛谷 P1514 引水入城

    这次不说闲话了,直接怼题 这道题用bfs其实并不难想,但比较困难的是怎么解决满足要求时输出蓄水厂的数量.其实就像其他题解说的那样,我们可以用bfs将它转化成一个区间覆盖问题,然后再进行贪心. 首先枚举 ...

  4. shell脚本,当用sed删除某一文件里面的内容时,并追加到同一个文件会出现问题。

    shell脚本,当用sed删除某一文件里面的内容时,并追加到同一个文件会出现问题.因为初始文件和写入文件是一个文件这是失败的.需要追加到另一个文件,然后再用mv进行操作.[root@localhost ...

  5. 漫谈使用Kafka作为MQ中间件

    哪些场景适合使用Kafka线上系统会实时产生数以万计的日志信息,服务器运行状态,用户行为记录,业务消息 等信息,这些信息需要用于多个不同的目的,比如审计.安全.数据挖掘等,因此需要以分类的方式将这些信 ...

  6. python 发送附件

    #!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油! #@file: sksendmail.py #@time: 2018/8/20 13:3 ...

  7. systemverilog之OOP

    what is oop terminology an example class default methods for classes static attibute assigment and c ...

  8. 手动搭建redis集群(3台)

    安装redis 1.搜索源中的redis包 apt-cache pkgnames | grep redis 2.安装redis-server apt-get install redis-server ...

  9. JQuery基本事件函数

    1,click单击事件 2,blur失去光标事件,focus获得光标事件 3,JQuery.on()函数:为html元素绑定事件,如下代码: $("div").on("c ...

  10. python基础学习笔记——os模块

    #OS模块 #os模块就是对操作系统进行操作,使用该模块必须先导入模块: import os #getcwd() 获取当前工作目录(当前工作目录默认都是当前文件所在的文件夹) result = os. ...