Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <string>
- #include <cctype>
- #include <stack>
- #include <queue>
- #include <list>
- #include <vector>
- #include <map>
- #include <sstream>
- #include <cmath>
- #include <bitset>
- #include <utility>
- #include <set>
- #define INT_MAX 2147483647
- #define INT_MIN -2147483648
- #define pi acos(-1.0)
- #define N 1000000
- #define long long LL
- using namespace std;
- int ugly[1501];
- int main()
- {
- int p2,p3,p5,a,b,c;
- ugly[1] = p2 = p3 = p5 = 1;
- for(int i=2; i < 1501 ; i++){
- a = 2* (ugly[p2]);
- b = 3* (ugly[p3]);
- c = 5* (ugly[p5]);
- if(a<b && a<c ){
- ugly[i] = a;//insert value
- p2++;//increase p2
- }
- else if(b<a && b<c ){
- ugly[i] = b;//insert value
- p3++;//increase p2
- }
- else if(c<a && c<b ){
- ugly[i] = c;//insert value
- p5++;//increase p2
- }
- else if(a == b){
- p3++;
- i--;//decrease i as not inserted
- }
- else if(a == c){
- p5++;
- i--;
- }
- else if(b == c){
- p5++;
- i--;
- }
- }
- //output
- cout << "The 1500'th ugly number is " << ugly[1500] << "." << endl;
- return 0 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement