-
Notifications
You must be signed in to change notification settings - Fork 0
/
136.cpp
60 lines (38 loc) · 1.05 KB
/
136.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <queue>
#include <algorithm>
#define MAXN 1500
using namespace std;
int counter;
int A[MAXN+1];
queue <int> q2, q3, q5;
int main( ) {
A[++counter] = 1;
q2.push( 2 );
q3.push( 3 );
q5.push( 5 );
while ( counter < MAXN ) {
int top = min( q2.front(), min( q3.front(), q5.front() ) );
while ( q2.front() < top )
q2.pop();
while ( q3.front() < top )
q3.pop();
while ( q5.front() < top )
q5.pop();
if ( q2.front() == top )
q2.pop();
if ( q3.front() == top )
q3.pop();
if ( q5.front() == top )
q5.pop();
A[++counter] = top;
q2.push( top*2.0 );
q3.push( top*3.0 );
q5.push( top*5.0 );
}
printf( "The 1500'th ugly number is %lld.\n", A[1500] );
return 0;
}