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 Specification

The input consists of one or more test cases. Each test case consists of one integer n with  . Input is terminated by a value of zero (0) for n.

Output Specification

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. 思路:本题要求第i小的质因子只有2,3,5,7的数,一个一个与质数相关的题目显然会超时,所以遇到这种问题一般会提前先进行预处理,之后查询。由于题目中说明n<=5842,最大的值为2*10^9所以可以断定本题使用枚举法将结果存在一个整形数组里即可。之后就是需要考虑枚举范围,2*10^9的是2^31,3^20,5^14,7^12以内,所以2、3、5、7的枚举范围分别为31,20,14,12即可。

在这里还有注意输出格式的问题,对于序数词:

1)若n%10=1,2,3并且n%100!=11,12,13,则末尾为st,nd,rd;

2)否则末尾为th;

AC代码:

#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#define MAX 2000000000
using namespace std; int hum[]; void getHum()
{
int a,b,c,d;
int len=;
long long num2,num3,num5,num7;
for(a=;a<=;a++)
{
num2=(long long)pow(2.0,(double)a);
for(b=;b<=;b++)
{
num3=(long long)pow(3.0,(double)b);
if(num2*num3>MAX)
break;
for(c=;c<=;c++)
{
num5=(long long)pow(5.0,(double)c);
if(num2*num3*num5>MAX)
break;
for(d=;d<=;d++)
{
num7=(long long)pow(7.0,(double)d);
if(num2*num3*num5*num7<=MAX)
hum[len++]=num2*num3*num5*num7;
else break;
}
}
}
}
sort(hum,hum+len);
} int main(void)
{
int n;
getHum();
while(scanf("%d",&n)==&&n!=)
{
if(n%==&&n%!=)
printf("The %dst humble number is %d.\n",n,hum[n-]);
else if(n%==&&n%!=)
printf("The %dnd humble number is %d.\n",n,hum[n-]);
else if(n%==&&n%!=)
printf("The %drd humble number is %d.\n",n,hum[n-]);
else
printf("The %dth humble number is %d.\n",n,hum[n-]);
}
return ;
}

参考博客:http://www.cnblogs.com/dolphin0520/archive/2011/04/15/2016774.html

SOJ1029 Humble Numbers (枚举)的更多相关文章

  1. 洛谷P2723 丑数 Humble Numbers

    P2723 丑数 Humble Numbers 52通过 138提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目背景 对于一给定的素数 ...

  2. Humble Numbers(丑数) 超详解!

    给定一个素数集合 S = { p[1],p[2],...,p[k] },大于 1 且素因子都属于 S 的数我们成为丑数(Humble Numbers or Ugly Numbers),记第 n 大的丑 ...

  3. USACO Humble Numbers

    USACO  Humble Numbers 这题主要是两种做法,第一种是比较常(jian)规(dan)的-------------用pq(priority_queue)维护,每次取堆中最小值(小根堆) ...

  4. [poj2247] Humble Numbers (DP水题)

    DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...

  5. HDU - The number of divisors(约数) about Humble Numbers

    Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence ...

  6. A - Humble Numbers

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Pract ...

  7. The number of divisors(约数) about Humble Numbers[HDU1492]

    The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  8. Humble Numbers

    Humble Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9988 Accepted: 4665 Descri ...

  9. HDU 1058 Humble Numbers (DP)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

随机推荐

  1. [android] 练习使用ListView(二)

    主要练习异步任务和LruCache缓存 package com.android.test; import java.io.InputStream; import java.net.HttpURLCon ...

  2. tomcat开启远程调试和热部署(jrebel)启动tomcat

    @echo off set REBEL_HOME=D:\jrebel\jrebel--nosetup set JAVA_OPTS=-agentpath:%REBEL_HOME%\lib\jrebel6 ...

  3. Linux From Scratch(从零开始构建Linux系统,简称LFS)(二)

    七. 构建临时系统 1. 通用编译指南 a. 确认是否正确设置了 LFS 环境变量 echo $LFS b. 假定你已经正确地设置了宿主系统需求和符号链接 c. 对于每个软件包: (1). 确保解压软 ...

  4. Chrome 声音自动播放抱错问题【play() failed】

    Chrome下调用play后抱错:DOMException: play() failed because the user didn't interact with the document firs ...

  5. 三大图表库:ECharts 、 BizCharts 和 G2,该如何选择?

    最近阿里正式开源的BizCharts图表库基于React技术栈,各个图表项皆采用了组件的形式,贴近React的使用特点.同时BizCharts基于G2进行封装,Bizcharts也继承了G2相关特性. ...

  6. Git 命令 操作

    常用 Git 命令清单 我每天使用 Git ,但是很多命令记不住.一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. 下面是我整理的常用 Git 命令清单. ...

  7. 任务九:使用HTML/CSS实现一个复杂页面

    任务目的 通过实现一个较为复杂的页面,加深对于HTML,CSS的实战能力 实践代码的复用.优化 任务描述 通过HTML及CSS实现设计稿 设计稿PSD文件(点击下载),效果如 效果图(点击打开) 整个 ...

  8. qt中qlineedit和qtextedit右键菜单翻译成中文

    没有linguist和lupdate等命令需要安装Linguist: 在Terminal中输入: sudo apt-get install qt4-dev-tools qt4-doc qt4-qtco ...

  9. *p++、*++p、(*p)++区别

    关于数组指针的谜题 假设 p 是指向数组 arr 中第 n 个元素的指针,那么 *p++.*++p.(*p)++ 分别是什么意思呢? *p++ 等价于 *(p++),表示先取得第 n 个元素的值,再将 ...

  10. Linux Firefox Adobe Flash Player 安装和更新

    1.下载 Firefox Adobe Flash Player 使用Linux上的火狐浏览器访问如下的下载网址: https://get.adobe.com/flashplayer/ 选择下载 &qu ...