问题描述:
我们把只包含因子2,3和5的数称做丑数。
实现代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
#define min(x,y,z) (x<y?x:y)<z?(x<y?x:y):z
int findUglyNumber(int n){
int rs[n];
rs[0]=1;
int i=1,flag2=0,flag3=0,flag5=0;
while(i<n){
int rs2 = rs[flag2]*2;
int rs3=rs[flag3]*3;
int rs5=rs[flag5]*5;
int minNum = min(rs2,rs3,rs5);
rs[i]=minNum;
while(rs2<=minNum){
flag2++;
rs2 = rs[flag2]*2;
}
while(rs3<=minNum){
flag3++;
rs3=rs[flag3]*3;
}
while(rs5<=minNum){
flag5++;
rs5=rs[flag5]*5;
}
i++;
}
return rs[n-1];
}
int main(int argc, char *argv[])
{
while(true){
int n;
scanf("%d",&n);
int num = findUglyNumber(n);
printf("%d\n",num);
}
return 0;
}
参考资料:
剑指offer
备注:
转载请注明出处:http://blog.csdn.net/wsyw126/article/details/51383773
作者:WSYW126