Poj 2247 Humble Numbers(求只能被2,3,5, 7 整除的数)
一、题目大意
本题要求写出前5482个仅能被2,3,5, 7 整除的数。
二、题解
这道题从本质上和Poj 1338 Ugly Numbers(数学推导)是一样的原理,只需要在原来的基础上加上7的运算即可。还有一个不同之处在于输出上,这个题要求第n的英语表示。而英语中的表示呢,如果n的个位数是1,用nst表示个位数是2的用,nnd表示;个位数是3的,用nrd表示。但是n的最后两位是11、12、13的还是用nth表示,其他的也是用th表示。
三、java代码
import java.util.Scanner;
public class Main {
public static String format(int n){
if(n % 10==1 && n% 100 !=11)
return n+"st";
if(n % 10==2 && n% 100 !=12)
return n+"nd";
if(n % 10==3 && n% 100 !=13)
return n+"rd";
return n+"th";
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n;
int i2_mul;
int i3_mul;
int i5_mul;
int i7_mul;
long[] ugly=new long[5843];
i2_mul = 1;
i3_mul = 1;
i5_mul = 1;
i7_mul = 1;
ugly[1]=1;
for( int i = 2; i <= 5842; i++ ){
ugly[i] = Math.min(Math.min(ugly[i2_mul]*2,
Math.min(ugly[i3_mul]*3,ugly[i5_mul]*5)), ugly[i7_mul]*7);
if(ugly[i] == ugly[i2_mul]*2 )
i2_mul++;
if(ugly[i] == ugly[i3_mul]*3 )
i3_mul++;
if(ugly[i] == ugly[i5_mul]*5)
i5_mul++;
if(ugly[i] == ugly[i7_mul]*7)
i7_mul++;
}
while((n=sc.nextInt())!=0){
System.out.println("The "+format(n)+" humble number is "+ugly[n]+".");
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Poj 2247 Humble Numbers(求只能被2,3,5, 7 整除的数)的更多相关文章
- POJ 2247 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, ...
- HDU-1492-The number of divisors(约数) about 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, ...
- DP 60题 -3 HDU1058 Humble Numbers DP求状态数的老祖宗题目
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 递推(三):POJ中的三道递推例题POJ 1664、POJ 2247和POJ 1338
[例9]放苹果(POJ 1664) Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. In ...
- A - Humble Numbers
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pract ...
- 洛谷P2723 丑数 Humble Numbers
P2723 丑数 Humble Numbers 52通过 138提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目背景 对于一给定的素数 ...
- HDU1492/The number of divisors(约数) about Humble Numbers
题目连接 The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- 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)维护,每次取堆中最小值(小根堆) ...
随机推荐
- linux c编程:信号(四) sigaction
signal 函数的使用方法简单,但并不属于 POSIX 标准,在各类 UNIX 平台上的实现不尽相同,因此其用途受到了一定的限制.而 POSIX 标准定义的信号处理接口是 sigaction 函数, ...
- IntelliJ IDEA(2017)安装和破解(转发)
IntelliJ IDEA(2017)安装和破解 IDEA 全称 IntelliJ IDEA,是Java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手 ...
- HDU - 1800 Flying to the Mars 【贪心】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1800 题意 给出N个人的 level 然后 高的level 的 人 是可以携带 比他低level 的人 ...
- Android selector背景选择器
selector根据不同的选定状态来定义不同的现实效果 常用属性: android:state_selected--------选中 android:state_focused--------获得焦点 ...
- CSS3分享按钮动画特效
在线演示 本地下载
- Spring Cloud之Swagger集群搭建
在微服务中,Swagger是每个服务 比如会员服务,订单服务,支付服务 进行继承. 如何将整个微服务中的Swagger进行合成,同一台服务器上. 使用Zuul+Swagger实现管理整个微服务API文 ...
- 算法(Algorithms)第4版 练习 1.5.1
id数组的变化情况: 0 1 2 3 4 5 6 7 8 9 10 components 9 0 0 1 2 3 4 5 6 7 8 0 9 components 3 4 0 1 2 4 5 6 7 ...
- tomcat7 中的坑。 关于welcome-list和servlet-mapping
web.xml中, 使用default servlet设置了针对静态资源后缀名的过滤. 并且设置了welcome-list, 使用jetty和tomcat6启动一切正常, 但是使用tomcat7则出现 ...
- c# 继承小结
本文意在巩固基础知识,并不是对其进行深入剖析,如若对各位高手没有什么作用,请绕过.本文为原创文,所有示例均是博主测试过的,欢迎大家批评指正,如有转载请标明出处,谢谢.继承.封装和多态是面向对象编程的重 ...
- User Agent注入攻击及防御
CloudFlare公司经常会收到客户询问为什么他们的一些请求会被 CloudFlare WAF屏蔽.最近,一位客户就提出他不能理解为什么一个访问他主页简单的 GET 请求会被 WAF 屏蔽. 下面是 ...