hdu 1058 Humble Numbers(优先队列+set)
来源:互联网 发布:微信回调域名 编辑:程序博客网 时间:2024/05/17 00:13
Humble Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21549 Accepted Submission(s): 9411
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.
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21549 Accepted Submission(s): 9411
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.
#include <limits.h>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <algorithm>#include <iostream>#include <iterator>#include <queue>#include <stack>#include <string>#include <vector>#include <set>//#define ONLINE_JUDGE#define eps 1e-8#define INF 0x7fffffff //INT_MAX#define inf 0x3f3f3f3f //int??????????????????#define FOR(i,a) for((i)=0;i<(a);(i)++) //[i,a);#define MEM(a) (memset((a),0,sizeof(a)))#define sfs(a) scanf("%s",a)#define sf(a) scanf("%d",&a)#define sfI(a) scanf("%I64d",&a)#define pf(a) printf("%d\n",a)#define pfI(a) printf("%I64d\n",a)#define pfs(a) printf("%s\n",a)#define sfd(a,b) scanf("%d%d",&a,&b)#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)#define for1(i,a,b) for(int i=(a);i<b;i++)#define for2(i,a,b) for(int i=(a);i<=b;i++)#define for3(i,a,b)for(int i=(b);i>=a;i--)#define MEM1(a) memset(a,0,sizeof(a))#define MEM2(a) memset(a,-1,sizeof(a))#define LL __int64const double PI = acos(-1.0);template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }template<class T> inline T Min(T a, T b) { return a < b ? a : b; }template<class T> inline T Max(T a, T b) { return a > b ? a : b; }using namespace std;template<class T>T Mint(T a, T b, T c) {if (a>b) {if (c>b)return b;return c;}if (c>a)return a;return c;}template<class T>T Maxt(T a, T b, T c) {if (a>b) {if (c>a)return c;return a;}else if (c > b)return c;return b;}const int maxn=6000;int T,n,m,k;LL f[maxn];int prime[]={2,3,5,7};void fun(){priority_queue<LL,vector<LL>,greater<LL> >q;set<LL>s;s.insert(1);q.push(1);for(int i=1;i<=5842;i++){LL x=q.top();f[i]=x;q.pop();for(int j=0;j<4;j++){LL x2=x*prime[j];if(!s.count(x2)){s.insert(x2);q.push(x2);}}}}int main() {#ifndef ONLINE_JUDGEfreopen("test.in","r",stdin);freopen("test.out","w",stdout);#endiffun();while(~sf(n)&&n){int t=n%10;int m=n%100/10;if(t==1&&m!=1){printf("The %dst humble number is %lld.\n",n,f[n]);}else if(t==2&&m!=1){printf("The %dnd humble number is %lld.\n",n,f[n]);}else if(t==3&&m!=1){printf("The %drd humble number is %lld.\n",n,f[n]);}else {printf("The %dth humble number is %lld.\n",n,f[n]);}}return 0;}
0 0
- hdu 1058 Humble Numbers(优先队列+set)
- HDU1058 Humble Numbers(优先队列+set)
- hdu 1058 Humble Numbers 丑数(set,优先队列应用)
- HDU 1058 Humble Numbers(丑数,优先队列,STL)
- Humble Numbers(优先队列)
- HDU 1058 Humble Numbers【巧用优先队列】
- hdu 1058 Humble Numbers (set)
- hdu1058 Humble Numbers【优先队列】
- Humble Numbers (HDU 1058)
- HDU 1058 Humble Numbers
- hdu 1058 Humble Numbers
- hdu 1058 Humble Numbers
- HDU 1058 Humble Numbers
- hdu 1058 Humble Numbers
- HDU 1058 Humble Numbers
- hdu 1058 humble numbers
- hdu 1058 Humble Numbers
- hdu 1058 Humble Numbers
- TS科普16 节目相关表PAT
- andriod 配置第三方模拟器出现genimotion打不开的情况
- gcc与g++
- 从照片库选择多张图片到collectionView
- canvas基础练习
- hdu 1058 Humble Numbers(优先队列+set)
- 详解Ajax技术
- LaTeX 矩阵
- UTF8、UTF16 之间的转换
- PHP开发工具及配置过程
- final
- IOS中类和对象还有,nil/Nil/NULL的区别
- MySql root 用户密码忘记后重置root密码
- 两条线段是否相交