题目:
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
shows the first 11 ugly numbers. By convention, 1 is included.
Write a program to find and print the 1500’th ugly number.
Input
There is no input to this program.
Output
Output should consist of a single line as shown below, with ‘<number>’ replaced by the number
computed.
Sample Output
The 1500'th ugly number is <number>.
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=72
题意:
找丑数,丑数只能被2或3或5整除,输出第1500个丑数(1包含在内)
AC代码:
#include<bits/stdc++.h>
int min(int a,int b)
{
return a>b?b:a;
}
int main()
{
int ugly[1505] = {1};
int n2=0,n3=0,n5=0;
int i;
for(i=1;i<1500;i++)
{
for(;n2<i;n2++)
if(ugly[n2]*2>ugly[i-1]) break;
for(;n3<i;n3++)
if(ugly[n3]*3>ugly[i-1]) break;
for(;n5<i;n5++)
if(ugly[n5]*5>ugly[i-1]) break;
ugly[i]=min(ugly[n2]*2,ugly[n3]*3);
ugly[i]=min(ugly[i],ugly[n5]*5);
}
cout<<"The 1500'th ugly number is"<< ugly[1499])<<endl;
return 0;
}
网友评论