由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Design an algorithm to find the kth number such that the only prime factors
相关主题
Amazon电面两题一个小公司面经
epi 还是 The Algorithm Design ManualOne question on Careercup
a problem from leetcode: high efficiency algorithm for combinations problemCoding test: you can get a job if you can provide a solution
问个G的intern面试google 面试题疑问
说说某著名软件公司的onsite面试问一道老题
Help, Algorithms questions问一A家题目
k-selection algorithman interview algorithm question about finding even occuring freq
征解几道large scale的数字题[cloudera面试] senior engineer
相关话题的讨论汇总
话题: int话题: design话题: kth话题: nmin话题: factors
进入JobHunting版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
Design an algorithm to find the kth number such that the only prime factors
are 3, 5 and 7.
w****x
发帖数: 2483
2
迭代法:
//Find the nth number which is composed by factor 2, 3 and 5
void PrintSerials(int n)
{
assert(n > 0);
vector vec;
vec.push_back(1);
int nI2 = 0;
int nI3 = 0;
int nI5 = 0;
int nCount = 1;
while(nCount < n)
{
int nTmp2 = vec[nI2]*2;
int nTmp3 = vec[nI3]*3;
int nTmp5 = vec[nI5]*5;
int nMin = min(min(nTmp2, nTmp3), nTmp5);
if (vec.back() != nMin)
{
vec.push_back(nMin);
nCount++;
}
if (nMin == nTmp2)
nI2++;
else if (nMin == nTmp3)
nI3++;
else nI5++;
}
for (vector::iterator it = vec.begin(); it != vec.end(); it++)
cout<<*it< }
c*********t
发帖数: 2921
3
ding

【在 c**********e 的大作中提到】
: Design an algorithm to find the kth number such that the only prime factors
: are 3, 5 and 7.

l******e
发帖数: 149
4
wwwyhx 的解法还是很牛的,简化了一下
static List getNth235(int k){
int i2 = 0, i3 = 0, i5 = 0;
List list = new ArrayList();
list.add(1);
while(list.size() < k){
int n2 = list.get(i2)*2;
int n3 = list.get(i3)*3;
int n5 = list.get(i5)*5;
int tmp = Math.min(Math.min(n2, n3), n5);
list.add(tmp);
if(tmp == n2) i2++;
if(tmp == n3) i3++;
if(tmp == n5) i5++;
}
return list;
}
c**********e
发帖数: 2007
5
Good solutions. Thank you both.
w****o
发帖数: 2260
6
wwwyhx,
没有完全看懂,能否讲讲你的idea?
你用了三个index, nI2, nI3, nI5,然后不断的调整这些index,到底每个index都指示的
是什么?
careercup 150题的哪本书上,是用了3个queue,你的做法和careercup的做法是如何等
效的?
其实这两个我都没有完全弄明白。
谢谢!

【在 w****x 的大作中提到】
: 迭代法:
: //Find the nth number which is composed by factor 2, 3 and 5
: void PrintSerials(int n)
: {
: assert(n > 0);
: vector vec;
: vec.push_back(1);
: int nI2 = 0;
: int nI3 = 0;
: int nI5 = 0;

w****o
发帖数: 2260
7
好像不对的。我试了一下 2, 3, 5 的情况,是错的。

【在 w****x 的大作中提到】
: 迭代法:
: //Find the nth number which is composed by factor 2, 3 and 5
: void PrintSerials(int n)
: {
: assert(n > 0);
: vector vec;
: vec.push_back(1);
: int nI2 = 0;
: int nI3 = 0;
: int nI5 = 0;

p*****2
发帖数: 21240
8
C++看着费劲。不过感觉不太好。
1 (共1页)
进入JobHunting版参与讨论
相关主题
[cloudera面试] senior engineer说说某著名软件公司的onsite面试
问到算法题和一道c++题Help, Algorithms questions
问道题: prime factork-selection algorithm
求解一道很难的算法面试题征解几道large scale的数字题
Amazon电面两题一个小公司面经
epi 还是 The Algorithm Design ManualOne question on Careercup
a problem from leetcode: high efficiency algorithm for combinations problemCoding test: you can get a job if you can provide a solution
问个G的intern面试google 面试题疑问
相关话题的讨论汇总
话题: int话题: design话题: kth话题: nmin话题: factors