hdu 1058 Humble Numbers
Problem Description
A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers.
Write a program to find and print the nth element in this sequence
Input
The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output.
Sample Input
1
2
3
4
11
12
13
21
22
23
100
1000
5842
0
Sample Output
The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.
URL:http://acm.hdu.edu.cn/showproblem.php?pid=1058
解题:
算法分析:典型的DP!
1 ->?
1 ->2=min(1*2,1*3,1*5,1*7)
1 ->2 ->3=min(2*2,1*3,1*5,1*7)
1 ->2 ->3 -> 4 = min(2*2,2*3,1*5,1*7)
1 ->2 ->3 -> 4 ->5= min(3*2,2*3,1*5,1*7)
状态转移方程
F(n)=min(F(i)*2,F(j)*3,F(k)*5,F(m)*7)(n>i,j,k,m)
特别的: i,j,k,m 只有在本项被选中后才移动
code:
代码一:
#include <algorithm>
using namespace std;
int f[5850];
int main()
{
int i,a, b, c, d;
a = b = c = d = 1;
for(i=1; i<8; i++)
f[i] = i;
for(i=8; i<5850; i++)
{
while(2*f[a]<= f[i-1])
a ++;
while(3*f[b]<= f[i-1])
b ++;
while(5*f[c]<= f[i-1])
c ++;
while(7*f[d]<= f[i-1])
d ++;
f[i] = min(2*f[a], min(3*f[b], min(5*f[c], 7*f[d])));
}
while(cin>>i, i)
{
cout<<"The "<<i;
if(i%10 == 1 && i%100 != 11)
cout<<"st";
else if(i%10 == 2 && i%100 != 12)
cout<<"nd";
else if(i%10 == 3 && i%100 != 13)
cout<<"rd";
else cout<<"th";
cout<<" humble number is "<<f[i]<<'.'<<endl;
}
return 0;
}
#include <string>
using namespace std;
const int MaxSize=5844 ;
__int64 num[MaxSize]={0,1,2,3,4,5,6,7,8}, avai[10]={2,3,5,7},humble[100] ;
void init()
{
int i, j, k ;
__int64 temp ;
bool flag ;
for( i=9; i<MaxSize; ++i )
num[i] = 0x7fffffff ;
for( i=9; i<MaxSize; ++i )
{
flag = false ;
for( j=0; j<4; ++j )
{
for( k=i-1; ; --k )
{
temp = num[k]*avai[j] ;
if( temp <= num[i-1] )
break ;
num[i] = min( num[i], temp ) ;
if( num[i]-num[i-1] == 1 )
{
flag = true ;
break ;
}
}
if( flag )
break ;
}
}
}
void output( int n )
{
printf("The %d", n ) ;
int last=n%100 ;
if( last==13 || last==12 || last==11 )
{
printf("th humble number is %I64d.\n", num[n] ) ;
return ;
}
last = n%10 ;
if( last == 1 )
printf("st") ;
else if( last == 2 )
printf("nd") ;
else if( last == 3 )
printf("rd");
else
printf("th") ;
printf(" humble number is %I64d.\n", num[n] ) ;
}
int main()
{
init() ;
int n ;
while( scanf("%d", &n), n )
{
output( n ) ;
}
return 0 ;
}
作者:BuildNewApp
出处:http://syxchina.cnblogs.com、 BuildNewApp.com
本文版权归作者、博客园和百度空间共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则作者会诅咒你的。
如果您阅读了我的文章并觉得有价值请点击此处,谢谢您的肯定1。