题目链接:点击打开链接
1010 只包含因子2 3 5的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 K的因子中只包含2 3 5。满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15。 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数。 例如:n = 13,S中 >= 13的最小的数是15,所以输出15。 Input 第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10000) 第2 - T + 1行:每行1个数N(1 <= N <= 10^18) Output 共T行,每行1个数,输出>= n的最小的只包含因子2 3 5的数。 Input示例 5 1 8 13 35 77 Output示例 2 8 15 36 80思路:注意 a[1] = 2 ; a[2] = 3 ; a[3] = 4 ; a[4] = 5 。
可以跟 杭电1058 比较一下
#include<cstdio> #include<algorithm> #include<cstring> #define LL long long using namespace std; const LL MAXN=1e18; LL n; LL a[1000010]; int k=0; void get() { a[0]=1; int e2=0,e3=0,e5=0; while(a[k]<=MAXN) { LL mmin=min(a[e2]*2,min(a[e3]*3,a[e5]*5)); a[++k]=mmin; if(mmin==a[e2]*2) e2++; if(mmin==a[e3]*3) e3++; if(mmin==a[e5]*5) e5++; } } int main() { get(); int t; scanf("%d",&t); while(t--) { scanf("%lld",&n); int pos=lower_bound(a+1,a+k+1,n)-a; // 查找范围不包括 a[0] printf("%lld\n",a[pos]); } return 0; }