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
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5878
思路:就是简单的单纯模拟,然后二分求出来,看了下,按照题目范围应该只有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
相关文章推荐
- 剑指offer34_丑数
- Ugly Number
- [C++]Ugly Number 丑数
- [C++]Ugly Number II丑数2
- 丑数
- 丑数(剑指offer)
- 【剑指Offer面试编程题】题目1214:丑数--九度OJ
- 【剑指offer】丑数
- 编程算法 - 丑数 代码(C)
- 寻找丑数
- POJ2545-丑数
- 寻找丑数(Ugly Number)
- 剑指offer的34题 丑数
- 算法习题64:寻找丑数2 3 5的倍数
- HDU 5878 I Count Two Three (暴力) 2016 ACM/ICPC Asia Regional Qingdao Online
- 剑指offer-丑数
- 剑指offer-5-面试34:丑数(优化时间和空间效率)
- 程序员面试题精选100题(37)-寻找丑数[算法]
- 剑指Offer----面试题34:丑数
- 丑数