http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010
http://poj.org/problem?id=1338
首先poj这题要找出第n个丑数,想到要打表,因为每一个丑数都是由前一个丑数*2或者*3或者*5得到,每次取3种结果种较小的那一个放入数组中.
打表的时候要注意保持数组有序.
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <vector> 5 #include <cstring> 6 #include <string> 7 #include <algorithm> 8 #include <string> 9 #include <set> 10 #include <functional> 11 #include <numeric> 12 #include <sstream> 13 #include <stack> 14 #include <map> 15 #include <queue> 16 //#pragma comment(linker, "/STACK:102400000,102400000") 17 #define CL(arr, val) memset(arr, val, sizeof(arr)) 18 19 #define ll long long 20 #define inf 0x7f7f7f7f 21 #define lc l,m,rt<<1 22 #define rc m + 1,r,rt<<1|1 23 #define pi acos(-1.0) 24 25 #define L(x) (x) << 1 26 #define R(x) (x) << 1 | 1 27 #define MID(l, r) (l + r) >> 1 28 #define Min(x, y) (x) < (y) ? (x) : (y) 29 #define Max(x, y) (x) < (y) ? (y) : (x) 30 #define E(x) (1 << (x)) 31 #define iabs(x) (x) < 0 ? -(x) : (x) 32 #define OUT(x) printf("%I64d ", x) 33 #define lowbit(x) (x)&(-x) 34 #define Read() freopen("a.txt", "r", stdin) 35 #define Write() freopen("b.txt", "w", stdout); 36 #define maxn 1010 37 #define maxv 1010 38 #define mod 1000000000 39 using namespace std; 40 ll p[1555]; 41 int main() 42 { 43 //freopen("a.txt","r",stdin); 44 int i=1,j=1,k=1; 45 p[1]=1; 46 for(int l=2;l<=1500;l++) 47 { 48 p[l]=min(p[i]*2,min(p[j]*3,p[k]*5)); 49 if(p[l]==p[i]*2) i++; 50 if(p[l]==p[j]*3) j++; 51 if(p[l]==p[k]*5) k++; 52 //printf("%lld ",p[l]); 53 } 54 int n; 55 while(~scanf("%d",&n)&&n) 56 { 57 printf("%lld ",p[n]); 58 } 59 return 0; 60 }
51nod这题让你求>=n的丑数,因为n很大,所以打完表后需要二分查找,打表的时候要注意直到大于10^18,上限可以自己试几次.
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <vector> 5 #include <cstring> 6 #include <string> 7 #include <algorithm> 8 #include <string> 9 #include <set> 10 #include <functional> 11 #include <numeric> 12 #include <sstream> 13 #include <stack> 14 #include <map> 15 #include <queue> 16 //#pragma comment(linker, "/STACK:102400000,102400000") 17 #define CL(arr, val) memset(arr, val, sizeof(arr)) 18 19 #define ll long long 20 #define inf 0x7f7f7f7f 21 #define lc l,m,rt<<1 22 #define rc m + 1,r,rt<<1|1 23 #define pi acos(-1.0) 24 25 #define L(x) (x) << 1 26 #define R(x) (x) << 1 | 1 27 #define MID(l, r) (l + r) >> 1 28 #define Min(x, y) (x) < (y) ? (x) : (y) 29 #define Max(x, y) (x) < (y) ? (y) : (x) 30 #define E(x) (1 << (x)) 31 #define iabs(x) (x) < 0 ? -(x) : (x) 32 #define OUT(x) printf("%I64d ", x) 33 #define lowbit(x) (x)&(-x) 34 #define Read() freopen("a.txt", "r", stdin) 35 #define Write() freopen("b.txt", "w", stdout); 36 #define maxn 1010 37 #define maxv 1010 38 #define mod 1000000000 39 using namespace std; 40 ll p[10999]; 41 42 ll erfen(ll n) 43 { 44 int l=1,r=10999; 45 while(l<=r) 46 { 47 int mid=(l+r)/2; 48 if(p[mid]==n) return p[mid]; 49 else if(p[mid]>n) r=mid-1; 50 else l=mid+1; 51 } 52 if(p[l]<n) l++; 53 return p[l]; 54 } 55 int main() 56 { 57 freopen("a.txt","r",stdin); 58 int i=1,j=1,k=1; 59 p[1]=1; 60 for(int l=2;l<=10999;l++) 61 { 62 p[l]=min(p[i]*2,min(p[j]*3,p[k]*5)); 63 if(p[l]==p[i]*2) i++; 64 if(p[l]==p[j]*3) j++; 65 if(p[l]==p[k]*5) k++; 66 //printf("%lld ",p[l]); 67 } 68 //printf("%lld ",p[10000]); 69 int t; 70 ll n; 71 scanf("%d",&t); 72 while(t--) 73 { 74 scanf("%lld",&n); 75 { 76 if(n==1) printf("2 "); 77 else 78 printf("%lld ",erfen(n)); 79 } 80 } 81 return 0; 82 }