HDU 5676 ztr loves lucky numbers (模拟)
ztr loves lucky numbers
题目链接:
http://acm.hust.edu.cn/vjudge/contest/121332#problem/I
Description
ztr 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 ztr came across a positive integer n. Help him to find the least super lucky number which is not less than n.
Input
There are T cases
For each cases:
The only line contains a positive integer . This number doesn't have leading zeroes.
Output
For each cases
Output the answer
Sample Input
2
4500
47
Sample Output
4747
47
题意:
定义super number:
有且仅有4和7两个数字,且两个数字出现的次数相同.
给出n,求最小的不小于n的super number
题解:
xjb打了一通大模拟,结果越打越觉得坑;
还好分清细节后过掉了;
模拟思路:考虑当前数位能是否能放4或7(用strcmp比较后续数);
当两数大小确定后,后面的序列应按最小顺序编排;
另解:
- 直接打表然后二分.
- 用next_permutation获取可能的组合再比较.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 3100
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
char num[25];
int cnt;
int main(int argc, char const *argv[])
{
//IN;
//freopen("out.txt","w",stdout);
// printf("%d\n", 1000000);
// for(int i=1; i<=1000000; i++)
// printf("%d\n", i);
int t; cin >> t; getchar();
//int t = 0;
while(t--)
{
LL n=0,cmps=0;
char c;
cnt = 0;
memset(num,0,sizeof(num));
gets(num);
for(int i=0; i<strlen(num); i++) {
if(num[i]<'0' || num[i]>'9') break;
cnt++;
n = n*10 + num[i] - '0';
}
num[cnt] = 0;
if(cnt&1) {
for(int i=1; i<=(cnt+1)/2; i++) printf("4");
for(int i=1; i<=(cnt+1)/2; i++) printf("7");
printf("\n");
continue;
}
for(int i=1; i<=cnt/2; i++) cmps = cmps*10 + 7;
for(int i=1; i<=cnt/2; i++) cmps = cmps*10 + 4;
if(cmps < n) {
for(int i=1; i<=(cnt+2)/2; i++) printf("4");
for(int i=1; i<=(cnt+2)/2; i++) printf("7");
printf("\n");
continue;
}
char ans[25] = {0};
int si=cnt/2,qi=cnt/2;
for(int i=0; i<cnt; i++) {
if(num[i]<'4') {
if(si) ans[i] = '4', si--;
else ans[i] = '7', qi--;
for(int j=0; j<=i; j++) printf("%c", ans[j]);
while(si--) printf("4");
while(qi--) printf("7");
printf("\n");
break;
}
if(num[i] == '4') {
char tmp[25] = {0};
for(int j=0; j<qi; j++) tmp[j] = '7';
for(int j=qi; j<qi+si-1; j++) tmp[j] = '4';
if(!si || strcmp(tmp,num+i+1) < 0) {
ans[i] = '7'; qi--;
for(int j=0; j<=i; j++) printf("%c", ans[j]);
while(si--) printf("4");
while(qi--) printf("7");
printf("\n");
break;
}
if(strcmp(tmp,num+i+1) == 0) {
ans[i] = '4'; si--;
for(int j=0; j<=i; j++) printf("%c", ans[j]);
printf("%s",tmp);
printf("\n");
break;
}
ans[i] = '4'; si--;
continue;
}
if(num[i] == '7') {
char tmp[25] = {0};
for(int j=0; j<qi-1; j++) tmp[j] = '7';
for(int j=qi-1; j<qi+si-1; j++) tmp[j] = '4';
if(!qi || strcmp(tmp,num+i+1) < 0) {
for(int i=1; i<=(cnt+2)/2; i++) printf("4");
for(int i=1; i<=(cnt+2)/2; i++) printf("7");
printf("\n");
break;
}
if(strcmp(tmp,num+i+1) == 0) {
ans[i] = '7'; qi--;
for(int j=0; j<=i; j++) printf("%c", ans[j]);
printf("%s",tmp);
printf("\n");
break;
}
ans[i] = '7'; qi--;
continue;
}
ans[i] = '7'; qi--;
for(int j=0; j<=i; j++)
printf("%c", ans[j]);
while(si--) printf("4");
while(qi--) printf("7");
printf("\n");
break;
}
//printf("\n");
}
return 0;
}
HDU 5676 ztr loves lucky numbers (模拟)的更多相关文章
- hdu 5676 ztr loves lucky numbers(dfs+离线)
Problem Description ztr loves lucky numbers. Everybody knows that positive integers are lucky if the ...
- 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
题目链接:hdu 5676 一开始看题还以为和数位dp相关的,后来才发现是搜索题,我手算了下,所有的super lucky number(也就是只含数字4, 7且4, 7的数量相等的数)加起来也不过几 ...
- HDU 5676 ztr loves lucky numbers【DFS】
题目链接; http://acm.hdu.edu.cn/showproblem.php?pid=5676 题意: 由4和7组成的且4和7出现次数相同的数称为幸运数字,给定n,求不大于n的最大幸运数字. ...
- hdu-5676 ztr loves lucky numbers(乱搞题)
题目链接: ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- ztr loves lucky numbers 傻逼的我来了个大模拟
http://acm.hdu.edu.cn/showproblem.php?pid=5676 这题的正解因该是dfs的,但是有18个位,然后我一算,全排列的话,有18!个啊,那不是很大?但是有很多是相 ...
- hdu5676 ztr loves lucky numbers(dfs)
链接 ztrloveslucky numbers 题意 定义幸运数为:只存在4和7且4和7数量相等的数,给出n,求比>=n的最小幸运数 做法 暴力搜出所有长度从2-18的幸运数,因为最多9个4, ...
- HDU 5677 ztr loves substring(Manacher+dp+二进制分解)
题目链接:HDU 5677 ztr loves substring 题意:有n个字符串,任选k个回文子串,问其长度之和能否等于L. 题解:用manacher算法求出所有回文子串的长度,并记录各长度回文 ...
- HDU 5675 ztr loves math (数学推导)
ztr loves math 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/A Description ztr loves re ...
随机推荐
- android多分辨率多屏幕密度下UI适配方案
相关概念 分辨率:整个屏幕的像素数目,为了表示方便一般用屏幕的像素宽度(水平像素数目)乘以像素高度表示,形如1280x720,反之分辨率为1280x720的屏幕,像素宽度不一定为1280 屏幕密度:表 ...
- 通过org.springframework.web.filter.CharacterEncodingFilter定义Spring web请求的编码
通过类org.springframework.web.filter.CharacterEncodingFilter,定义request和response的编码.具体做法是,在web.xml中定义一个F ...
- UVa 11572 Unique snowflakes【滑动窗口】
题意:给出 n个数,找到尽量长的一个序列,使得该序列中没有重复的元素 看的紫书,滑动窗口来做的 当右端碰到有相同的数的时候,左端向前滑动一个数 模拟一个样例好理解些 #include<iostr ...
- SQL复制表及表结构
复制表结构和数据SQL语句 1:复制表结构及数据到新表 select * into 目的数据库名.dbo.目的表名 from 原表名 select * into my0735home.dbo.info ...
- NoSQL开篇——为什么要使用NoSQL
NoSQL在2010年风生水起,大大小小的Web站点在追求高性能高可靠性方面,不由自主都选择了NoSQL技术作为优先考虑的方面.今年伊始,InfoQ中文站有幸邀请到凤凰网的孙立先生,为大家分享他之于N ...
- JQuery Ajax 在asp.net中使用小结
自从有了JQuery,Ajax的使用变的越来越方便了,但是使用中还是会或多或少的出现一些让人短时间内痛苦的问题.本文暂时总结一些在使用JQuery Ajax中应该注意的问题,如有不恰当或者不完善的地方 ...
- django --------------------- [必要操作]
基本models 命令: python manage.py validate (验证模型有效性, 记得配置 settings.py - INSTALLED_APPS) python manage.py ...
- ffmpeg显示视频
项目最近需要实现播放视频功能,这个在上家公司就做过.虽然跟之前的场景不一样,有以前的功底还是很快可以解决,事实也确实如此.在使用DShow处理完视频分割与合并后,继续使用DShow显示视频,很快即完成 ...
- 【策略】HDOJ-1205-吃糖果
[题目链接:HDOJ-1205] 思路:直接看题毫无思路... 看了别人的思路,到现在还懵懵懂懂. 只要除了数目最多的糖果以外的其他所有糖果的数目之和加1(小心这里要用int64),大于等于数目最 ...
- 【转】出現 "PowerCam player support IE browser only!" 的錯誤訊息
原文网址:http://www.camdemy.com/faq/1138 A. 這是由於新版 IE11 針對文件模式設定的改變,衍生 PowerCam5 及6 的教材閱讀問題 ( EverCam 已 ...