http://poj.org/problem?id=1338
写写水题放松放松- -#
#include<iostream> #include<queue> using namespace std; typedef pair<unsigned long,int>node_type; int main(void) { unsigned long result[1500]; priority_queue<node_type,vector<node_type>,greater<node_type> >Q; Q.push(make_pair(1,2)); for(int i=0;i<1500;i++) { node_type node=Q.top(); Q.pop(); switch(node.second) { case 2:Q.push(make_pair(node.first*2,2)); case 3:Q.push(make_pair(node.first*3,3)); case 5:Q.push(make_pair(node.first*5,5)); } result[i]=node.first; } int n; while(cin>>n&&n) cout<<result[n-1]<<endl; return 0; }
上面是新写的用STL实现。
#include<cstdio> #include<algorithm> using namespace std; int main(void) { int a[1501],x,y,z,n,i; x=y=z=a[1]=1; for(i=2;i<=1500;i++) { a[i]=min(2*a[x],min(3*a[y],5*a[z])); if(2*a[x]==a[i])x++; if(3*a[y]==a[i])y++; if(5*a[z]==a[i])z++; } while(scanf("%d",&n),n) printf("%d\n",a[n]); return 0; }