日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

poj 1338 Ugly Numbers(丑数模拟)

發(fā)布時(shí)間:2023/12/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 1338 Ugly Numbers(丑数模拟) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/u012860063?

viewmode=contents

題目鏈接: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:用一個(gè)長(zhǎng)度為1500的數(shù)組存儲(chǔ)這些數(shù),另有三個(gè)游標(biāo)x,y,z;


a[1]=1。x=y=z=1,代表第一個(gè)數(shù)為1,此后的數(shù)都是通過(guò)已有的數(shù)乘以2,3,5得到的,
那么x,y,z分別代表a[x],a[y],a[z]能夠通過(guò)乘以2,3,5來(lái)得到新的數(shù),i遞增。每次取2*a[x], 3*a[y], 5*a[z]
中的最小值。得到a[i]后。能夠?qū)⑾鄳?yīng)的x(或y,z)右移,當(dāng)然假設(shè)原本通過(guò)3*2得到6,那么2*3也能得到6,
因此可能x和y都須要遞增。


詳見(jiàn)代碼:

#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; }


轉(zhuǎn)載于:https://www.cnblogs.com/yxwkf/p/5371179.html

總結(jié)

以上是生活随笔為你收集整理的poj 1338 Ugly Numbers(丑数模拟)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。