SOJ1029 Humble Numbers (枚举)
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 (枚举)的更多相关文章
- 洛谷P2723 丑数 Humble Numbers
P2723 丑数 Humble Numbers 52通过 138提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目背景 对于一给定的素数 ...
- Humble Numbers(丑数) 超详解!
给定一个素数集合 S = { p[1],p[2],...,p[k] },大于 1 且素因子都属于 S 的数我们成为丑数(Humble Numbers or Ugly Numbers),记第 n 大的丑 ...
- USACO Humble Numbers
USACO Humble Numbers 这题主要是两种做法,第一种是比较常(jian)规(dan)的-------------用pq(priority_queue)维护,每次取堆中最小值(小根堆) ...
- [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 ...
- 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 ...
- A - Humble Numbers
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pract ...
- The number of divisors(约数) about Humble Numbers[HDU1492]
The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- Humble Numbers
Humble Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9988 Accepted: 4665 Descri ...
- HDU 1058 Humble Numbers (DP)
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
随机推荐
- XAML语法及标记扩展、附加属性、特定的字符和空白
1.对象元素语法 使用开闭标签将对象定义一个XML元素,这种语法与其他标记语言如HTML的元素语法非常相似,在以左右尖括号保卫要设置的类或结构的类型名称.对象元素可以声明0个或多个属性,以一个或多个空 ...
- svn在commit后报错:is scheduled for addition, but is missing
今天通过svn 的cr(code review)代码审核后,我欲执行svn ci -m"xxxxxxx(提交注释) ISSUE=3380305",但是没有提交成功,SVN报错啦! ...
- .net IoC 之 Spring.Net 适合刚开始使用
Spring.Net包括控制反转(IoC) 和面向切面(AOP),这篇文章主要说下IoC方面的入门. 一.首先建立一个MVC项目名称叫SpringDemo,然后用NuGet下载spring(我用的是S ...
- Oracle中Merge into的用法实例讲解
最近在做一个需求,就是涉及到表的问题,前端传过来一条数据,根据主键,查询数据库,如果不存在,那么久插入到数据库中一条,如果存在的话,就是以主键的方式,对数据库中的数据,进行更新. 拿到这个需求的时候, ...
- sharepint 2013 添加subsite
在用服务器端对象模型往里面添加subsite的时候,照着书上的代码,结果,失败.报错 not suported language. bing了半天,说是语言未支持,又是修改系统区域,显示语言等,还是失 ...
- Tips——单页面内的多重跳转路由使用
一.问题背景 一个路由往往代表一个地址,即一个页面.但同级网页页面的内容有很多是重复的,如果每次加载页面都要加载这些“共有”内容,会导致效率的降低.因此,单页面应用应运而生.它主张在同一页面下将“共同 ...
- python caser运行编码
#!/usr/bin/env python# -*- coding:utf-8 -*-import os def encryption(): str_raw = raw_input("请输入 ...
- 初识shell expect
场景:工作中经常会遇到shell脚本写的连接脚本,所以稍微了解下. 一.shell Shell 是一个用C语言编写的程序,它是用户使用Linux的桥梁.Shell既是一种命令语言,又是一种程序设计语言 ...
- VueJS开发所用到的技术栈
1. 主要使用vue.js2. 使用vue-cli脚手架搭建项目3. 使用vue-router来做路由,实现单页面跳转4. 使用iView UI作为前端UI框架,Mouse UI作为手机端UI框架5. ...
- typeof的探讨
console.log(typeof 'abc') // "string" console.log(typeof true )// "boolean" cons ...