很簡單的一道題,但是我竟然蠢到想不明白為什么如果從頭生成會出現大量重復的數字。
寫的時候主要出現的錯誤在爆int上,一定要注意數據范圍。
#include <iostream>
#include <queue>
#include <set>using namespace std;
using ll = long long;int main() {ios::sync_with_stdio(false);priority_queue<ll, vector<ll>, greater<ll>> pq;set<ll> s;pq.push(1);s.insert(1);auto deal = [&](ll x) -> void {if (s.count(x)) return;s.insert(x);pq.push(x);};ll x;for (int i = 1; ; ++i) {x = pq.top();pq.pop();if (i == 1500) {cout << "The 1500'th ugly number is " << x << ".\n";break;}deal(x * 2);deal(x * 3);deal(x * 5);}return 0;
}