Ugly Numbers--POJ 1338
2010-07-14 18:04
405 查看
1、解题思路:数论、STL。
2、注意事项:i++使用,跟踪i的值;STL的简单应用。
3、实现方法:(复杂版)
4、实现方法:(STL精简版)
2、注意事项:i++使用,跟踪i的值;STL的简单应用。
3、实现方法:(复杂版)
#include <iostream> using namespace std; int UglyNumbers[1500]; int MinNumber(int a,int b,int c) { int min=a; if(min>b) min=b; if(min>c) min=c; return min; } void Init() { int two=0,three=0,five=0,temp; UglyNumbers[0]=1; for(int i=1;i<1500;i++) { temp=MinNumber(UglyNumbers[two]*2,UglyNumbers[three]*3,UglyNumbers[five]*5); if(temp==UglyNumbers[two]*2) two++; if(temp==UglyNumbers[three]*3) three++; if(temp==UglyNumbers[five]*5) five++; UglyNumbers[i]=temp; } } int seach(int x) { return UglyNumbers[x-1]; } int main() { int n; Init(); while(cin>>n && n) { cout<<seach(n)<<endl; } return 0; }
4、实现方法:(STL精简版)
#include<algorithm> #include<iostream> #include<queue> using namespace std; typedef pair<unsigned long,int> node_type; int main() { 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 1;}
相关文章推荐
- POJ1338 Ugly Numbers
- POJ 1338 Ugly Numbers
- POJ-1338 Ugly Numbers-被2,3,5整除的数
- Poj 1338 Ugly Numbers(数学推导)
- POJ 1338 Ugly Numbers
- POJ 1338 Ugly Numbers
- poj 1338 Ugly Numbers(丑数模拟)
- poj 1338 Ugly Numbers【终极打表】
- poj 1338 ugly numbers和poj 2247Humble Numbers 解题报告(附详细分析)
- POJ 1338 Ugly Numbers 笔记
- POJ 1338 Ugly Numbers(丑数)
- poj 1338 Ugly Numbers 枚举素因子幂
- Poj 1338 Ugly Numbers(数学推导)
- POJ 1338 Ugly Numbers
- POJ 1338 Ugly Numbers(数论)
- poj1338 Ugly Numbers(技巧)
- POJ 1338 Ugly Numbers 寻找丑数
- poj 1338 Ugly Numbers(丑数模拟)
- (DS1.5.8)POJ 1338 Ugly Numbers(寻找因子只含有2||3||5的数)
- POJ1338 Ugly Numbers 丑数 [数论]