http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010
http://poj.org/problem?id=1338
首先poj这题要找出第n个丑数,想到要打表,因为每一个丑数都是由前一个丑数*2或者*3或者*5得到,每次取3种结果种较小的那一个放入数组中.
打表的时候要注意保持数组有序.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1010
#define maxv 1010
#define mod 1000000000
using namespace std;
ll p[];
int main()
{
//freopen("a.txt","r",stdin);
int i=,j=,k=;
p[]=;
for(int l=;l<=;l++)
{
p[l]=min(p[i]*,min(p[j]*,p[k]*));
if(p[l]==p[i]*) i++;
if(p[l]==p[j]*) j++;
if(p[l]==p[k]*) k++;
//printf("%lld\n",p[l]);
}
int n;
while(~scanf("%d",&n)&&n)
{
printf("%lld\n",p[n]);
}
return ;
}
51nod这题让你求>=n的丑数,因为n很大,所以打完表后需要二分查找,打表的时候要注意直到大于10^18,上限可以自己试几次.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1010
#define maxv 1010
#define mod 1000000000
using namespace std;
ll p[]; ll erfen(ll n)
{
int l=,r=;
while(l<=r)
{
int mid=(l+r)/;
if(p[mid]==n) return p[mid];
else if(p[mid]>n) r=mid-;
else l=mid+;
}
if(p[l]<n) l++;
return p[l];
}
int main()
{
freopen("a.txt","r",stdin);
int i=,j=,k=;
p[]=;
for(int l=;l<=;l++)
{
p[l]=min(p[i]*,min(p[j]*,p[k]*));
if(p[l]==p[i]*) i++;
if(p[l]==p[j]*) j++;
if(p[l]==p[k]*) k++;
//printf("%lld\n",p[l]);
}
//printf("%lld\n",p[10000]);
int t;
ll n;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
{
if(n==) printf("2\n");
else
printf("%lld\n",erfen(n));
}
}
return ;
}