hdu 5676 ztr loves lucky numbers(BC——暴力打表+二分查找)
2016-05-02 21:22
387 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5676
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 594 Accepted Submission(s): 257
Problem 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(1≤n≤105) cases
For each cases:
The only line contains a positive integer n(1≤n≤1018).
This number doesn't have leading zeroes.
Output
For each cases
Output the answer
Sample Input
2
4500
47
Sample Output
4747
47
Source
BestCoder Round #82 (div.2)
题目大意:
解题思路:暴力打出所有幸运数~~~~然后二分查找即可。
详见代码。
#include <iostream>
#include <cstdio>
using namespace std;
#define ll long long
ll a[100000];
int k=0;
void dfs(ll ans,int num4,int num7)
{
if (num4==0&&num7==0)
{
a[k++]=ans;
return;
}
if (num4==0)
{
dfs(ans*10+7,num4,num7-1);
}
else if (num7==0)
{
dfs(ans*10+4,num4-1,num7);
}
else
{
dfs(ans*10+4,num4-1,num7);
dfs(ans*10+7,num4,num7-1);
}
}
int main()
{
int t;
scanf("%d",&t);
for (int i=2;i<=18;i+=2)
dfs(0,i/2,i/2);
while (t--)
{
ll n;
scanf("%lld",&n);
if(n>777777777444444444LL)
{
printf("44444444447777777777\n");
continue;
}
int l=0,r=k;
while (r>l)
{
int mid=(l+r)/2;
// cout<<mid<<endl;
if (a[mid]<n)
{
l=mid+1;
}
else
{
r=mid;
}
}
printf("%lld\n",a[r]);
}
return 0;
}
ztr loves lucky numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 594 Accepted Submission(s): 257
Problem 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(1≤n≤105) cases
For each cases:
The only line contains a positive integer n(1≤n≤1018).
This number doesn't have leading zeroes.
Output
For each cases
Output the answer
Sample Input
2
4500
47
Sample Output
4747
47
Source
BestCoder Round #82 (div.2)
题目大意:
ztr喜欢幸运数字,他对于幸运数字有两个要求 1:十进制表示法下只包含4、7 2:十进制表示法下4和7的数量相等 比如47,474477就是 而4,744,467则不是 现在ztr想知道最小的但不小于n的幸运数字是多少
解题思路:暴力打出所有幸运数~~~~然后二分查找即可。
详见代码。
#include <iostream>
#include <cstdio>
using namespace std;
#define ll long long
ll a[100000];
int k=0;
void dfs(ll ans,int num4,int num7)
{
if (num4==0&&num7==0)
{
a[k++]=ans;
return;
}
if (num4==0)
{
dfs(ans*10+7,num4,num7-1);
}
else if (num7==0)
{
dfs(ans*10+4,num4-1,num7);
}
else
{
dfs(ans*10+4,num4-1,num7);
dfs(ans*10+7,num4,num7-1);
}
}
int main()
{
int t;
scanf("%d",&t);
for (int i=2;i<=18;i+=2)
dfs(0,i/2,i/2);
while (t--)
{
ll n;
scanf("%lld",&n);
if(n>777777777444444444LL)
{
printf("44444444447777777777\n");
continue;
}
int l=0,r=k;
while (r>l)
{
int mid=(l+r)/2;
// cout<<mid<<endl;
if (a[mid]<n)
{
l=mid+1;
}
else
{
r=mid;
}
}
printf("%lld\n",a[r]);
}
return 0;
}
相关文章推荐
- 常用类
- 【BZOJ4403】序列统计【Lucas定理】
- [可并堆 中位数] BZOJ 1367 [Baltic2004]sequence
- 最大流 POJ 1273
- Mysql的远程连接
- HDU 5128 The E-pang Palace
- Java接口
- ACTOR模型和CSP模型
- 图形用户界面(GUI)应用程序开发——菜鸟的第一步
- 设计模式之模版方法模式
- ubuntu常见问题
- Object-c------自定义构造方法
- iOS 8 WKWebView
- 用栈写的计算器
- BLE 读取characteristic时,为什么没有 'BLE_GATTS_EVT_READ' ?
- ansible之file模块
- 283. Move Zeroes
- Ubuntu下ZeroMQ安装与使用
- Intellij IDEA开发(local模式)提交运行Spark代码
- 第六七章读后感