HDU--1058--Humble Numbers

来源:互联网 发布:知乎作死经历 编辑:程序博客网 时间:2024/06/06 09:11

Description

A number whoseonly 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 thefirst 20 humble numbers. 

Write a program to find and print the nth element in this sequence 

 

Input

The inputconsists of one or more test cases. Each test case consists of one integer nwith 1 <= n <= 5842. Input is terminated by a value of zero (0) for n. 

 

Output

For each testcase, print one line saying "The nth humble number is number.". Dependingon the value of n, the correct suffix "st", "nd","rd", or "th" for the ordinal number nth has to be usedlike 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 humblenumber is 2.

The 3rd humblenumber is 3.

The 4th humblenumber is 4.

The 11th humblenumber is 12.

The 12th humblenumber is 14.

The 13th humblenumber is 15.

The 21st humblenumber is 28.

The 22nd humblenumber is 30.

The 23rd humblenumber is 32.

The 100th humblenumber is 450.

The 1000thhumble number is 385875.

The 5842ndhumble number is 2000000000.

       

 

没想到递归..发现暴力可以过..

#include<stdio.h>#include<algorithm>#include<iostream>#include<string>#include<string.h>#include<set>#include<math.h>using namespace std;int main(){set<int> a;set<int>::iterator it;int n;long long a1[6000];int i,j,k,p;for(i=0;pow(2,i)<=2000000000;i++)for(j=0;pow(2,i)*pow(3,j)<=2000000000;j++)for(k=0;pow(2,i)*pow(3,j)*pow(5,k)<=2000000000;k++)for(p=0;pow(2,i)*pow(3,j)*pow(5,k)*pow(7,p)<=2000000000;p++)a.insert(pow(2,i)*pow(3,j)*pow(5,k)*pow(7,p));for(i=1,it=a.begin();it!=a.end();it++,i++)a1[i]=*it;while(cin>>n&&n){if(n%100!=11&&n%10==1) printf("The %dst humble number is %lld.\n",n,a1[n]); else if(n%100!=12&&n%10==2) printf("The %dnd humble number is %lld.\n",n,a1[n]); else if(n%100!=13&&n%10==3) printf("The %drd humble number is %lld.\n",n,a1[n]); else printf("The %dth humble number is %lld.\n",n,a1[n]);}}