Created
May 13, 2019 13:47
-
-
Save jongha/807bbb4db51740bad1d4727039cac530 to your computer and use it in GitHub Desktop.
UVa 136 - Ugly Numbers, https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=72
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <vector> | |
#include <list> | |
#include <algorithm> | |
#include <string.h> | |
#include <string> | |
#include <queue> | |
#include <stack> | |
#include <math.h> | |
#include <set> | |
#define FIND 1500 | |
// 2, 3, 5 | |
// 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... | |
int main(void) { | |
int m[1501]; | |
int pos = 0; | |
m[pos] = 1; | |
while(pos < FIND) { | |
int p2 = 0, p3 = 0, p5 = 0; | |
while(m[pos] >= m[p2] * 2) ++p2; | |
while(m[pos] >= m[p3] * 3) ++p3; | |
while(m[pos] >= m[p5] * 5) ++p5; | |
++pos; | |
if(m[pos-1] < m[p2] * 2) m[pos] = m[p2] * 2; | |
if(m[pos-1] < m[p3] * 3 && m[pos] > m[p3] * 3) m[pos] = m[p3] * 3; | |
if(m[pos-1] < m[p5] * 5 && m[pos] > m[p5] * 5) m[pos] = m[p5] * 5; | |
} | |
/* | |
for(int i=0; i<FIND; ++i) { | |
printf("%d\n", m[i]); | |
}*/ | |
printf("The %d'th ugly number is %d.\n", FIND, m[pos - 1]); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment