#include <cstdio> #include <iostream> #include <string> using namespace std; #ifdef ONLINE_JUDGE #define FINPUT(file) 0 #define FOUTPUT(file) 0 #else #define FINPUT(file) freopen(file,"r",stdin) #define FOUTPUT(file) freopen(file,"w",stdout) #endif long long int uglyNumber[1510]; long long int mymin(long long int a,long long int b,long long int c) { return a < b ? (a<c ? a:c):(b<c ? b:c); } void getUglyNumber() { uglyNumber[0] = 1; int curIndex = 1; long long int *m3 = uglyNumber; long long int *m2 = uglyNumber; long long int *m5 = uglyNumber; while(curIndex<1500) { int Min = mymin(*m2*2,*m3*3,*m5*5); uglyNumber[curIndex] = Min; while(*m2 * 2<=uglyNumber[curIndex]) m2++; while(*m3 * 3<=uglyNumber[curIndex]) m3++; while(*m5 * 5<=uglyNumber[curIndex]) m5++; curIndex++; } } int main() { FINPUT("in.txt"); FOUTPUT("out.txt"); getUglyNumber(); int n; while(cin>>n && n) { cout<<uglyNumber[n-1]<<endl; } return 0; }
原文地址:http://blog.csdn.net/daringpig/article/details/25618891