poj 1338 Ugly Numbers(丑数模拟)
2024-08-31 05:50:07
转载请注明出处: 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.
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;
}
最新文章
- iOS 学习 - 22 异步解析 JSON,使用 FMDB 存储,TableView 显示
- Android_helloworld
- EHCACHE采用分布需要注意的地方
- Env:Cscope安装与配置
- 知问前端——对话框UI(一)
- JAVA JDK 1.6 API中文版.CHM打开chm提示,“ 已取消到该网页的导航”
- apue
- [AngularJS] Transforming raw JSON data to meaningful output in AngularJS
- javascript中数组循环的方式
- Swift学习字符串、数组、字典
- 04 | 链表(上):如何实现LRU缓存淘汰算法?
- js怎么获取微信浏览器内容的高度
- [luogu2522][bzoj2301][HAOI2011]Problem b【莫比乌斯反演】
- linux 考试2
- 工具-CocoaPods安装和使用及卸载
- idea 上搭建 Mybatis 逆向工程
- Javap -c 字节码解析
- AME_AME审批中子元素的概念和用途(概念)
- Vertica7 Native Connection Load Balance
- RT-thread内核之消息队列
热门文章
- adb remount 失败remount failed: Operation not permitted
- 获取apk信息工具(android SDK的aapt工具)
- ASP.NET导出excel表方法汇总
- 通过简单的Linux内核启动程序代码窥探操作系统的启动原理
- 如何将自定义RPM包加入YUM
- 多元线性回归(Linear Regression with multiple variables)与最小二乘(least squat)
- Hadoop-Map/Reduce实现实现倒排索引
- Javascript原理
- 读取jar内的配置文件
- NodeJS:树的反序列化