Description
Petya 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 Petya came across a positive integer n. Help him to find the least super lucky number which is not less than n.
The only line contains a positive integer n (1≤n≤109). This number doesn't have leading zeroes.
Output the least super lucky number that is more than or equal to n.
Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.
4500
4747
47
47
题目的意思就是给出一个数找出大于等于这个数的最小数(这个数由4和7组成)
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s1[1000],s2="";
int flag=0;
for(int i=1;i<6;i++)
{
s2.clear();
for(int j=0;j<i;j++)
s2.push_back('4');
for(int j=0;j<i;j++)
s2.push_back('7');
do
{
s1[flag++]=s2;
//cout<<s1[flag-1]<<endl;
}while(next_permutation(s2.begin(),s2.end()));
//cout<<flag<<endl;
}
long long int a[1000];
for(int i=0;i<flag;i++)
{
sscanf(s1[i].c_str(),"%lld",&a[i]);
}
long long int n;
cin>>n;
printf("%lld\n",a[lower_bound(a,a+flag,n)-a]);
return 0;
}
分析:
a.用到了next_permutation() 这个是全排列一个用法,以前没用过
使用方法就是上述代码的形式,
b.用到了string 原本自己用的是vector但是有个问题就是把一维数组赋值给二维数组是错误的
c. s.c_str()就是为了把c++和c兼容而搞出来的这里的sscanf()是c里的
所以要转换一下
int: +-2147483647 十位 这里最大是5个7和5个4所以用long long int
本文介绍了一个算法问题,即寻找大于等于给定数的最小“超幸运数”。超幸运数是一种特殊的正整数,其十进制表示仅包含4和7,并且4和7的数量相等。文中提供了一种解决方案,通过生成所有可能的超幸运数并对其进行排序来找到符合条件的最小数。
975

被折叠的 条评论
为什么被折叠?



