# POJ 2247 && HDU 1058 Humble Numbers（数论）

325人阅读 评论(0)

Description

Input

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.
Solution

Code

#include<stdio.h>
#define min(x,y) x<y?x:y
int main()
{
int i,u2,u3,u5,u7,n;
long int ugly[6000];
ugly[1]=1;
u2=1;
u3=1;
u5=1;
u7=1;
for(i=2;i<6000;i++)
{
ugly[i]=min(ugly[u3]*3,ugly[u5]*5);
ugly[i]=min(ugly[i],ugly[u2]*2);
ugly[i]=min(ugly[i],ugly[u7]*7);
if(ugly[i]==ugly[u2]*2)
u2++;
if(ugly[i]==ugly[u3]*3)
u3++;
if(ugly[i]==ugly[u5]*5)
u5++;
if(ugly[i]==ugly[u7]*7)
u7++;
}
while(scanf("%d",&n)&&n)
{
if(n%100>10&&n%100<20)
printf("The %dth humble number is %d.\n",n,ugly[n]);
else if(n%10==1)
printf("The %dst humble number is %d.\n",n,ugly[n]);
else if(n%10==2)
printf("The %dnd humble number is %d.\n",n,ugly[n]);
else if(n%10==3)
printf("The %drd humble number is %d.\n",n,ugly[n]);
else
printf("The %dth humble number is %d.\n",n,ugly[n]);
}
}

0
0

个人资料
• 访问：556897次
• 积分：24582
• 等级：
• 排名：第313名
• 原创：1943篇
• 转载：0篇
• 译文：0篇
• 评论：69条
阅读排行
最新评论