题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n) 解题 法一:直接暴力,逐次判断一个数是不是丑数 下面只对其中的奇数判断是否是丑数,加不加奇数都超时. class Solution { /** * @param k: The number k. * @return: The kth prime number as description. */…
问题描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 思路1:(显然是比较耗时的) 直接去判断每个整数是不是丑数,然后找到第N个小的数.(牛客网提交超时) public int GetUglyNumber_Solution(int index) { if(index <= 0){ return 0; } int number = 0; int found =…
hdu1058 题意:当一个数只有2.3.5.7这四种质因数时(也可以一种都没有或只有其中几种),这个数就是丑数,输出第 n 个丑数是多少: 其实并没有发现hdu把这道题放在 dp 专题里的意图,我的思路就是预处理出丑数数组,然后读入 n 就直接输出第 n 个丑数.我自己又一种想法,询问翔神之后又学到了另一种做法. 我自己的生成数组的方法是:数组的第一个元素定为1,然后用优先队列,首先将2,3,5,7放入优先队列,每次从队列中取出最小的一个数,将它放入数组中,然后分别将它乘上2,3,5,7后放入…
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include<vector> using namespace std; class Solution { public: int GetUglyNumber_Solution(int index) { int Max = 0;//记录最大丑数 int num = 0; vector<…
题目:http://acm.timus.ru/problem.aspx?space=1&num=1748 题意:求n范围内约数个数最多的那个数. Roughly speaking, for a number to be highly composite it has to have prime factors as small as possible, but not too many of the same. If we decompose a number n in prime factor…
Save Princess 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Yesterday, the princess was kidnapped by a devil. The prince has to rescue our pretty princess. "OK, if you want to save the beautiful princess, you must answer my questions correctly."the…