标签:humble numbers 穷举 ugly number相同思路
The 5842nd humble number is 2000000000.
#include<iostream>
using
namespace
std;
long
int
x[5900];
int
num2=0,num3=0,num5=0,num7=0;
void
t()
{
x[0]=1;
for
(
int
i=1;i<5900;i++)
{
int
min=x[num2]*2,b=x[num3]*3,c=x[num5]*5,d=x[num7]*7;
if
(min>b)
{
min=b;
}
if
(min>c)
{
min=c;
}
if
(min>d)
{
min=d;
}
x[i]=min;
if
(x[i]==x[num2]*2)
num2++;
if
(x[i]==x[num3]*3)
num3++;
if
(x[i]==x[num5]*5)
num5++;
if
(x[i]==x[num7]*7)
num7++;
}
}
int
main()
{
int
n;
t();
while
(cin>>n&&n!=0)
{
if
(n%10==1&&n!=11)
cout<<
"The"
<<
"
"
<<n<<
"st"
<<
"
"
<<
"humble
number is "
<<x[n-1]<<
"."
<<endl;
else
if
(n%10==2&&n!=12)
cout<<
"The"
<<
"
"
<<n<<
"nd"
<<
"
"
<<
"humble
number is "
<<x[n-1]<<
"."
<<endl;
else
if
(n%10==3&&n!=13)
cout<<
"The"
<<
"
"
<<n<<
"rd"
<<
"
"
<<
"humble
number is "
<<x[n-1]<<
"."
<<endl;
else
cout<<
"The"
<<
"
"
<<n<<
"th"
<<
"
"
<<
"humble
number is "
<<x[n-1]<<
"."
<<endl;
}
return
0;
}
一直因子只能是2,3,5,7,所以分别用四个量来表示,通过将最小值放入数组中构造成功
另外需要考虑输出形式,在11,12,13处分别有小陷阱需注意
标签:humble numbers 穷举 ugly number相同思路
原文地址:http://blog.csdn.net/u013240812/article/details/25830615