Ugly Number II(求第N个丑数)

来源:互联网 发布:如何通过网络发送短信 编辑:程序博客网 时间:2024/05/16 00:36
int getmin(int a,int b,int c){    if(a>b)    {        if(b>c)        return c;        else        return b;    }    else     {        if(a>c)        return c;        return a;    }}class Solution {public:    int nthUglyNumber(int n) {        if(n==1)        return 1;        int i,j,k;      int a[100000];      int pos1=0;      int pos2=0;      int pos3=0;      int factor1=2,factor2=3,factor3=5;      a[0]=1;        for(i=1;i<n;i++)        {           a[i]=getmin(factor1,factor2,factor3);                                 if(factor1==a[i])           {               factor1=2*a[++pos1];           }            if(factor2==a[i])           {               factor2=3*a[++pos2];           }           if(factor3==a[i])           {               factor3=5*a[++pos3];           }                  }        return a[n-1];    }};

0 0
原创粉丝点击