日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

[POJ1338]Ugly Numbers

發布時間:2024/4/17 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [POJ1338]Ugly Numbers 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[POJ1338]Ugly Numbers

試題描述

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.

輸入

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

輸出

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

輸入示例

1 2 9 0

輸出示例

1 2 10

數據規模及約定

見“輸入

題解

首先與處理一下前 1500 個“丑數”,建立一個堆,對于一個“丑數” x,我們把 x * 2, x * 3, x * 5 都扔進堆,輸出一下發現居然沒有爆 unsigned long long(事實上連 int 都沒爆),所以就是思博題了。

查詢就訪問數組即可。

#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack> #include <vector> #include <queue> #include <cstring> #include <string> #include <map> #include <set> using namespace std;const int BufferSize = 1 << 16; char buffer[BufferSize], *Head, *Tail; inline char Getchar() {if(Head == Tail) {int l = fread(buffer, 1, BufferSize, stdin);Tail = (Head = buffer) + l;}return *Head++; } int read() {int x = 0, f = 1; char c = Getchar();while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }return x * f; }#define maxn 1510 #define ULL unsigned long long int n; ULL num[maxn]; priority_queue <ULL> Q;int main() {num[1] = 1;Q.push(-2ll); Q.push(-3ll); Q.push(-5ll);for(int i = 2; i <= 1500; i++) {num[i] = -Q.top(); Q.pop();while(num[i] == num[i-1]) num[i] = -Q.top(), Q.pop();Q.push(-(num[i] << 1ll)); Q.push(-num[i] * 3ll); Q.push(-num[i] * 5ll);}while(1) {n = read();if(!n) break;printf("%llu\n", num[n]);}return 0; }

?

轉載于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5803054.html

總結

以上是生活随笔為你收集整理的[POJ1338]Ugly Numbers的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。