Ugly Numbers
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11120 | Accepted: 4886 |
Description
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, ...
shows the first 10 ugly numbers. By convention, 1 is included.
Given the integer n,write a program to find and print the n'th ugly number.
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...
shows the first 10 ugly numbers. By convention, 1 is included.
Given the integer n,write a program to find and print the n'th ugly number.
Input
Each line of the input contains a postisive integer n (n <= 1500).Input is terminated by a line with n=0.
Output
For each line, output the n’th ugly number .:Don’t deal with the line with n=0.
Sample Input
1 2 9 0
Sample Output
1 2 10
Source
New Zealand 1990 Division I,UVA 136
我的求解:
Ugly number 都是有2、3、5作为因子组成得(1除外),可以采用暴力的方法搜索一定数量的Ugly number 后排序。相关数据参考得到!
我的代码:
#include <iostream>
#include <algorithm>
#include <algorithm>
using namespace std;
__int64 calUglyNum( int i, int j, int k )
{
__int64 sum=1;
int x1=2,x2=3,x3=5;
{
__int64 sum=1;
int x1=2,x2=3,x3=5;
while( i>=0 || j>=0 || k>=0 )
{
if( sum >1000000000000)
return 0;
if( i<=0 ) x1=1;
if( j<=0 ) x2=1;
if( k<=0 ) x3=1;
{
if( sum >1000000000000)
return 0;
if( i<=0 ) x1=1;
if( j<=0 ) x2=1;
if( k<=0 ) x3=1;
sum = sum*x1*x2*x3;
i--;j--;k--;
}
i--;j--;k--;
}
return sum;
}
}
int main()
{
__int64 uglyNum[9000];
{
__int64 uglyNum[9000];
int cnt =0;
int i,j,k;
for( i=0; i<30; i++ )
{
for( j=0; j<20; j++ )
{
for( k=0; k<15; k++ )
{
__int64 temp = calUglyNum(i,j,k);
if( temp>0 )
{
uglyNum[cnt] = temp;
cnt++;
}
if( cnt>=9000 )
{
break;
}
}
if( cnt>=9000 )
break;
}
if( cnt>=9000)
break;
}
int i,j,k;
for( i=0; i<30; i++ )
{
for( j=0; j<20; j++ )
{
for( k=0; k<15; k++ )
{
__int64 temp = calUglyNum(i,j,k);
if( temp>0 )
{
uglyNum[cnt] = temp;
cnt++;
}
if( cnt>=9000 )
{
break;
}
}
if( cnt>=9000 )
break;
}
if( cnt>=9000)
break;
}
sort( uglyNum, uglyNum+cnt-1 );
int n;
while( cin>>n && n!=0 )
{
printf("%I64d/n",uglyNum[n-1]);
}
return 0;
}
int n;
while( cin>>n && n!=0 )
{
printf("%I64d/n",uglyNum[n-1]);
}
return 0;
}
方法二:
#include <iostream.h>
#define N 1500
unsigned long s[N]; // s数组 存储结果 数量不会超过1500
void main ()
{
int now, idx2, idx3, i;
unsigned long val2, val3, val5;
s[0]=1; // 第一个满足条件的整数1
now = 0; // 当前搜索的满足条件整数的索引值 初始为0
val2=1; val3=1; val5=1; //
idx2=0; idx3=0; //
while (now < N-1) // 不会大于1500 s数组索引
{
/*
* 整体思路是一个迭代的过程 从1开始 然后乘以 2 或 3 或 5
* 需要保证s是个按索引递增的数组
*/
// 选择val2 、val3、val5中的最小值赋值给s[now]
if( val2 <= val3 && val2 <= val5) // 如果val2为其中最小值 计算因子含有2的数
{
if (val2>s[now]) // 保证s[now] 大于s[now-1]
s[++now]=val2;
val2 = 2*s[idx2++]; // 更新val2
}
else if(val3 <= val5) // val3最小
{
if(val3>s[now]) // 保证s[now] 大于s[now-1]
s[++now]=val3; // 将val3的值赋给s[++now]
/*
* while循环 搜索因子不含2的数 计算因子含有3或5的数
*/
while(s[idx3++] % 2 == 0); // 剪枝过程 ,没有这条语句一样的结果 但是这时应把idx3-1 改成 index++
val3 = 3*s[idx3-1];
cout<<s[idx3-1]<<" ";
}
else // val5最小
{
if(val5 >s[now])
s[++now]=val5;
val5 *= 5; // 计算因子全是5的数
}
}
cout<<"符合条件的数从小到大依次为:"<<endl;
for(i=0;i<1500;i++)
{
cout<<s[i]<<" ";
}
}
#define N 1500
unsigned long s[N]; // s数组 存储结果 数量不会超过1500
void main ()
{
int now, idx2, idx3, i;
unsigned long val2, val3, val5;
s[0]=1; // 第一个满足条件的整数1
now = 0; // 当前搜索的满足条件整数的索引值 初始为0
val2=1; val3=1; val5=1; //
idx2=0; idx3=0; //
while (now < N-1) // 不会大于1500 s数组索引
{
/*
* 整体思路是一个迭代的过程 从1开始 然后乘以 2 或 3 或 5
* 需要保证s是个按索引递增的数组
*/
// 选择val2 、val3、val5中的最小值赋值给s[now]
if( val2 <= val3 && val2 <= val5) // 如果val2为其中最小值 计算因子含有2的数
{
if (val2>s[now]) // 保证s[now] 大于s[now-1]
s[++now]=val2;
val2 = 2*s[idx2++]; // 更新val2
}
else if(val3 <= val5) // val3最小
{
if(val3>s[now]) // 保证s[now] 大于s[now-1]
s[++now]=val3; // 将val3的值赋给s[++now]
/*
* while循环 搜索因子不含2的数 计算因子含有3或5的数
*/
while(s[idx3++] % 2 == 0); // 剪枝过程 ,没有这条语句一样的结果 但是这时应把idx3-1 改成 index++
val3 = 3*s[idx3-1];
cout<<s[idx3-1]<<" ";
}
else // val5最小
{
if(val5 >s[now])
s[++now]=val5;
val5 *= 5; // 计算因子全是5的数
}
}
cout<<"符合条件的数从小到大依次为:"<<endl;
for(i=0;i<1500;i++)
{
cout<<s[i]<<" ";
}
}