很简单的一道题,但是我竟然蠢到想不明白为什么如果从头生成会出现大量重复的数字。
写的时候主要出现的错误在爆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;
}