Design an algorithm to find the kth number such that the only prime factors
Design an algorithm to find the kth number such that the only prime factors# JobHunting - 待字闺中
c*e
1 楼
Design an algorithm to find the kth number such that the only prime factors are 3, 5 and 7.
w*x
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<}
c*t
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
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; }