一、题目大意

本题要求写出前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 整除的数)的更多相关文章

  1. 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, ...

  2. 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, ...

  3. DP 60题 -3 HDU1058 Humble Numbers DP求状态数的老祖宗题目

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

  4. 递推(三):POJ中的三道递推例题POJ 1664、POJ 2247和POJ 1338

    [例9]放苹果(POJ 1664) Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. In ...

  5. A - Humble Numbers

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

  6. 洛谷P2723 丑数 Humble Numbers

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

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

    题目连接 The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory L ...

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

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

  9. USACO Humble Numbers

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

随机推荐

  1. 小程序网络请求arraybuffer 转为base64

    wx.request({ url: result.tempFilePath, method: 'GET', responseType: 'arraybuffer', success: function ...

  2. JS中如何获取<Select>中value和text的值

    原文地址:JS中如何获取<Select>中value和text的值 html代码: <select id = "city" onchange="chan ...

  3. 【leetcode刷题笔记】Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  4. Vue组件通信(传值)

    先介绍一下什么是组件把: 创建组件的两种方式: 全局组件 // 组件就是vue的一个拓展实例 let component=Vue.extend({ data(){ return{ //与vue实例中的 ...

  5. hd acm1061

    Problem Description Given a positive integer N, you should output the most right digit of N^N.   Inp ...

  6. vim 的visual可视模式

    一,在普通模式下面可以按v或者V进入可视模式下,选择内容: v 可以选择光标位置到光标结束的字符,包括行: V 选择光标位置行到光标结束的所在行的之间的所有行,选择的是个矩形: CTRL+v 选择块:

  7. Kuhn-Munkres算法 (剪辑)(备用)

    KM算法是通过给每个顶点一个标号(叫做顶标)来把求最大权匹配的问题转化为求完备匹配的问题的. 设顶点Xi的顶标为A[i],顶点Yi的顶标为B[i],顶点Xi与Yj之间的边权为w[i,j]. 在算法执行 ...

  8. 关于使用unigui、webxone、mysql的几个问题

    一.webxone的问题:1.目前稳定可用的版本好像是v2510:2.设计webxone应用时,窗口的“position”属性只能设置为poDesigned,而且不能动态改变窗口尺寸,否则在浏览器中显 ...

  9. 对unidbgrid的单元格操作

    一.使某行某列单元格disabled: 1. UniStringGrid -> Options -> goEditing = true 2. UniStringGrid -> Ext ...

  10. SQL嵌套子查询和相关子查询的执行过程有什么区别(推荐)

    SQLServer子查询可以分为 相关子查询 和 嵌套子查询 两类.前提, 假设Books表如下: 类编号 图书名 出版社 价格 ----------------------------------- ...