转自:http://blog.sina.com.cn/s/blog_9dfc6f5f01015nu7.html
诺西笔试最后一道题,题意:
把只包含质因子2、3和5的数称作丑数(Ugly Number),例如:2,3,4,5,6,8,9,10,12,15,等,习惯上我们把1当做是第一个丑数。#include <iostream> using namespace std; int mymin(int a, int b, int c) { int temp = (a < b ? a : b); return (temp < c ? temp : c); } int FindUgly(int n) // { int* ugly = new int[n]; ugly[0] = 1; int index2 = 0; int index3 = 0; int index5 = 0; int index = 1; while (index < n) { int val = mymin(ugly[index2]*2, ugly[index3]*3, ugly[index5]*5); //竞争产生下一个丑数 if (val == ugly[index2]*2) //将产生这个丑数的index*向后挪一位; ++index2; if (val == ugly[index3]*3) //这里不能用elseif,因为可能有两个最小值,这时都要挪动; ++index3; if (val == ugly[index5]*5) ++index5; ugly[index++] = val; } int result = ugly[n-1]; delete[] ugly; return result; } int main() { int num=1; printf("input the number: \n"); scanf("%d", &num); printf("%d \n",FindUgly(num)); return 0; }