转载请注明出处:

viewmode=contents">http://blog.csdn.net/u012860063?

viewmode=contents

题目链接:

id=1338">http://poj.org/problem?id=1338

Description

Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 

1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ... 

shows the first 10 ugly numbers. By convention, 1 is included. 

Given the integer n,write a program to find and print the n'th ugly number. 

Input

Each line of the input contains a postisive integer n (n <= 1500).Input is terminated by a line with n=0.

Output

For each line, output the n’th ugly number .:Don’t deal with the line with n=0.

Sample Input

1
2
9
0

Sample Output

1
2
10

PS:用一个长度为1500的数组存储这些数,另有三个游标x,y,z;

a[1]=1。x=y=z=1,代表第一个数为1,此后的数都是通过已有的数乘以2,3,5得到的,

那么x,y,z分别代表a[x],a[y],a[z]能够通过乘以2,3,5来得到新的数,i递增。每次取2*a[x], 3*a[y], 5*a[z]

中的最小值。得到a[i]后。能够将相应的x(或y,z)右移,当然假设原本通过3*2得到6,那么2*3也能得到6,

因此可能x和y都须要递增。

详见代码:

#include <iostream>
using namespace std;
int min(int a, int b, int c)
{
return min(a,min(b,c));
}
int main()
{
int a[1517];
int x, y, z, i;
x = y = z = 1, a[1] = 1;
for(i = 2; i <= 1500; i++)
{
a[i] = min(2*a[x],3*a[y],5*a[z]);
if(a[i] == 2*a[x])
x++;
if(a[i] == 3*a[y])
y++;
if(a[i] == 5*a[z])
z++;
}
int n;
while(cin >> n && n)
{
cout<<a[n]<<endl;
}
return 0;
}

poj 1338 Ugly Numbers(丑数模拟)的更多相关文章

  1. Poj 1338 Ugly Numbers(数学推导)

    一.题目大意 本题要求写出前1500个仅能被2,3,5整除的数. 二.题解 最初的想法是从1开始检验该数是否只能被2,3,5整除,方法是这样的,对于一个数,如果它能被2整除,就除以2,如果它能被3整除 ...

  2. 51nod 1010 只包含因子2 3 5的数 && poj - 1338 Ugly Numbers(打表)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010 http://poj.org/problem?id=1338 首先 ...

  3. lintcode :Ugly Numbers 丑数

    题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n ...

  4. poj 1338 Ugly Numbers

    原题链接:http://poj.org/problem?id=1338 优先队列的应用,如下: #include<cstdio> #include<cstdlib> #incl ...

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

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

  6. Ugly number丑数2,超级丑数

    [抄题]: [思维问题]: [一句话思路]:Long.valueOf(2)转换为long型再做 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图 ...

  7. 263 Ugly Number 丑数

    编写程序判断给定的数是否为丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 6, 8 是丑数,而 14 不是,因为它包含了另外一个质因子 7.注意:    1 也可以被当做丑数.    输 ...

  8. Poj 2247 Humble Numbers(求只能被2,3,5, 7 整除的数)

    一.题目大意 本题要求写出前5482个仅能被2,3,5, 7 整除的数. 二.题解 这道题从本质上和Poj 1338 Ugly Numbers(数学推导)是一样的原理,只需要在原来的基础上加上7的运算 ...

  9. LeetCode OJ:Ugly Number II(丑数II)

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

随机推荐

  1. ANDROID开发之SQLite详解

    本文转自:http://www.cnblogs.com/Excellent/archive/2011/11/19/2254888.html

  2. MBR与GRUB简介

    在坛子里找到一篇关于grub和mbr工作原理的文章,以前一直都是一头雾水,今天转这文章学习下..哈.. 能正常工作的grub应该包 括一下文件:stage1.stage2.*stage1_5.menu ...

  3. util-判断当前年份所处的季度,并返回当前季度开始的月份

    ylbtech-funcation-util:  判断当前年份所处的季度,并返回当前季度开始的月份 判断当前年份所处的季度,并返回当前季度开始的月份. 1.A,Ylbtech.Model返回顶部 us ...

  4. 手势解锁自定义View

    package com.rxx.view; import java.util.ArrayList; import java.util.List; import java.util.Timer; imp ...

  5. bjfu1238 卡特兰数取余

    题目就是指定n,求卡特兰数Ca(n)%m.求卡特兰数有递推公式.通项公式和近似公式三种,因为要取余,所以近似公式直接无法使用,递推公式我简单试了一下,TLE.所以只能从通项公式入手. Ca(n) = ...

  6. <转>如何测试大型ERP软件?

    大型ERP软件是一个在企业范围内部应用的.高度集成的软件,且操作频繁,数据在各业务系统之间高度共享.那么针对大型ERP软件的特点,我们应该怎么测试呢?要使用怎样的测试方法?需要什么样的测试人员?... ...

  7. 从python run 和python unittest两种eclipse运行方式深入理解if __name__ == "__main__"

    在写一个简单的python测试程序的时候,发现eclipse中Run as "Python run 和 Python unittest”结果不一样?为什么会不一样? 先贴一下代码段: # - ...

  8. DataGrid的打印预览和打印

    using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System. ...

  9. 对象指针与this指针

    对象指针分为三大类 [1]指向对象的指针 [2]指向对象成员的指针(数据类) [3]指向对象成员的指针(函数类) #include<iostream> using namespace st ...

  10. python中struct模块及packet和unpacket

    转自:http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html 我们知道python只定义了6种数据类型,字符串,整数,浮点数,列表,元组 ...