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. 深入理解JavaScript系列(42):设计模式之原型模式

    介绍 原型模式(prototype)是指用原型实例指向创建对象的种类,并且通过拷贝这些原型创建新的对象. 正文 对于原型模式,我们可以利用JavaScript特有的原型继承特性去创建对象的方式,也就是 ...

  2. SQL SERVER 2012修改数据库名称(包括 db.mdf 名称的修改)

    假设原来数据库名为db,附加数据库为db.mdf和db_log.ldf.需要改成dbt,及dbt.mdf和dbt_log.ldf. 步骤: .首先把原来的数据库进行备份(选择数据库->右键-&g ...

  3. cocos-creator 脚本逻辑-2

    1.预制体 1)节点操作 Cc.find(‘node-1’) 获取节点 全局事件 作用于 canvas this.node.destroy() 删除节点(从内存中删除) 添加删除获取节点或组件 let ...

  4. 软件项目技术点(5)——在canvas上绘制动态网格线

    AxeSlide软件项目梳理   canvas绘图系列知识点整理 grid类的实现 当鼠标在画布上缩放时,网格能跟着我的鼠标滚动而相应的有放大缩小的效果. 下面是具体实现的代码,draw函数里计算出大 ...

  5. Linux基础之-利用shell脚本实现自动监控系统服务

    目的:监控集群内nginx及nfs服务运行是否正常,如任一服务异常,则发送邮件通知用户 条件: 1. 主机及子机IP地址,hostname已确定: 2. 主机与子机能够免密通讯,即基于密匙通讯(相关命 ...

  6. vs2017源文件创建代码自动版权声明注释

    原来在vs2015下用的挺好,顺便移植到2017下. 用文本打开,在其头部加上 “C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterpr ...

  7. sqlite 时间函数及时间处理

    SQLite分页显示:Select * From news order by id desc Limit 10 Offset 10这篇文章是根据 SQLite 官方 WIKI 里的内容翻译,如果有什么 ...

  8. css层叠性和继承性

    1.了解css层叠性 层叠性是什么?就是解决处理css选择器和属性冲突的能力.css的选择器权重是分大小,就是当多个选择器都选中了同一个标签时,听谁的??? 标签选择器 < 类选择器 < ...

  9. 从数据库读取数据后将其输出成html标签

    最常用的方法,使用JS或JQ JQ: $("#div").html("<span>我是HTML代码</span>"); JS: var ...

  10. Build 2016: 发布明天的云创新来服务今天的开发者

    每个企业和行业都在被云潜移默化地改变着.随着云计算的速度.规模和灵活性的不断增加,云服务带来的可能性也在不断被拓展.想象一下,通过监测传感器,一位奶农能够将他的奶牛牛奶产量提高:或是一家医院能够自动监 ...