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比较后续数);

当两数大小确定后,后面的序列应按最小顺序编排;

另解:

  1. 直接打表然后二分.
  2. 用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;
}

最新文章

  1. 程序员用HTML5给女朋友制作的3D相册
  2. 使用Sublime Text3开发AngularJs
  3. 12个免费的 Twitter Bootstrap 后台模板
  4. ios 图片的两种加载方式
  5. 《2016ThoughtWorks技术雷达峰会----变革的原因》
  6. Hash哈希(一)
  7. Atitit..组件化事件化的编程模型--(2)---------Web datagridview 服务器端控件的实现原理and总结
  8. HDU 4888 (网络流)
  9. Mac环境下装node.js,npm,express;(包括express command not found)
  10. winform 五子棋 判断输赢 分类: WinForm 2014-08-07 20:55 256人阅读 评论(0) 收藏
  11. UESTC_王之盛宴 2015 UESTC Training for Graph Theory&lt;Problem K&gt;
  12. SQL Server中日志
  13. Linux磁盘和文件系统管理
  14. [Swift]LeetCode827. 最大人工岛 | Making A Large Island
  15. phantomjs 中文文档
  16. 3. Dubbo原理解析-Dubbo内核实现之动态编译 (转)
  17. MySQL 插入emoji 表情
  18. php 文件上传类,功能相当齐全,留作开发中备用吧。
  19. ubuntu 安装MySQLdb
  20. linux第七周

热门文章

  1. Effective C++学习笔记 条款02:尽量以const,enum,inline替换 #define
  2. unique() 去重函数
  3. hdu 2986 Ballot evaluation (模拟)
  4. 一台电脑同时运行多个tomcat配置方法
  5. 函数get_table_share
  6. UVa 12096 The SetStack Computer【STL】
  7. bq24075 锂电池 充电电路分析
  8. 晶振波形、MIPI波形
  9. dict 字典
  10. Cadence原理图与Allegro交互