您的位置:首页 > 其它

hdu 5878 I Count Two Three 丑数

2016-09-17 20:13 323 查看
题意:求第一个大于等于n的最小丑数

思路:就是简单的单纯模拟,然后二分求出来,看了下,按照题目范围应该只有5195个丑数。有个注意的地方,判断等于那里不能写成else if,因为这样会造成大量重复的丑数,直接爆掉

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5878

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

long long ans[100000];
int cnt;

long long Min(long long a, long long b, long long c, long long d)
{
long long temp1 = a < b ? a : b;
long long temp2 = c < d ? c : d;
return temp1 < temp2 ? temp1 : temp2;
}

void init()
{
cnt = 1;
int a1 = 0, a2 = 0, a3 = 0, a4 = 0;
ans[0] = 1;
while(true)
{
long long temp = Min(ans[a1] * 2, ans[a2] * 3, ans[a3] * 5, ans[a4] * 7);
ans[cnt++] = temp;
if(temp == ans[a1] * 2) a1++;
if(temp == ans[a2] * 3) a2++;//不能写成else if
if(temp == ans[a3] * 5) a3++;
if(temp == ans[a4] * 7) a4++;
if(ans[cnt - 1] > 1000000000)
break;
}
}

int main()
{
init();
int t;
scanf("%d", &t);
while(t--)
{
long long n;
scanf("%I64d", &n);
int pos = lower_bound(ans, ans + cnt, n) - ans;
printf("%I64d\n", ans[pos]);
}
return 0;
}


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5878
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息