丑数运算:
先说一下解题的部分知识点:
迭代器中元素距离关系:
#include <iostream>
#include <list>
using namespace std;
int main () {
list<int> mylist;
for (int i=0; i<10; i++) mylist.push_back (i*10);
list<int>::iterator first = mylist.begin();
list<int>::iterator last = mylist.end();
list<int>::iterator it = first;
for(;it != last;++it)
cout<<"第"<<distance(first,it)<<"个元素的值为:"<<*it<<endl;
return 0;
}
输出:
第0个元素的值为:0
第1个元素的值为:10
第2个元素的值为:20
第3个元素的值为:30
第4个元素的值为:40
第5个元素的值为:50
第6个元素的值为:60
第7个元素的值为:70
第8个元素的值为:80
第9个元素的值为:90
题目描述:
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, …
shows the first 10 ugly numbers. By convention, 1 is included.
Given the integer n,write a program to find and print the n’th ugly number.
Input
Each line of the input contains a postisive integer n (n <= 1500).Input is terminated by a line with n=0.
Output
For each line, output the n’th ugly number .:Don’t deal with the line with n=0.
Sample Input
1
2
9
0
Sample Output
1
2
10
非本题解:
输入多行,输出多行(输出丑数为n的下标)
#include <iostream>
#include<cstdio>
#include<string>
#include <algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<vector>
int a[2000];
typedef long long LL;
const int base[]={2,3,5};
using namespace std;
int main()
{
int n,j=1;
priority_queue<LL,vector<LL>,greater<LL> >pq;
set<LL>s;
s.insert(1);
pq.push(1);
int cnt = 0;
while(1)
{
LL x=pq.top();pq.pop();
cnt++;
if(cnt==1500)
{
break;
}
LL tmp;
for(int i=0;i<3;++i)
{
tmp=x*base[i];
if(s.count(tmp)==0)
{
s.insert(tmp);
pq.push(tmp);
}
}
}
while(cin>>n)
{
if(n==0)
break;
set<LL>::iterator it;
set<LL>::iterator first=s.begin();
for(it=s.begin();it!=s.end();it++)
{
if(n==*it)
{
a[j]=distance(first,it)+1;
j++;
continue;
}
}
}
for(int i=1;i<=j-1;i++)
cout<<a[i]<<endl;
return 0;
}
非本题解:
输入一行,输出一行(输出为丑数为n的下标)
#include <iostream>
#include<cstdio>
#include<string>
#include <algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<vector>
int a[2000];
typedef long long LL;
const int base[]={2,3,5};
using namespace std;
int main()
{
int n,j=1;
priority_queue<LL,vector<LL>,greater<LL> >pq;
set<LL>s;
s.insert(1);
pq.push(1);
int cnt = 0;
while(1)
{
LL x=pq.top();pq.pop();
cnt++;
if(cnt==1500)
{
break;
}
LL tmp;
for(int i=0;i<3;++i)
{
tmp=x*base[i];
if(s.count(tmp)==0)
{
s.insert(tmp);
pq.push(tmp);
}
}
}
while(cin>>n)
{
if(n==0)
break;
set<LL>::iterator it;
set<LL>::iterator first=s.begin();
for(it=s.begin();it!=s.end();it++)
{
if(n==*it)
{
a[j]=distance(first,it)+1;
cout<<a[j]<<endl;
j++;
continue;
}
}
}
return 0;
}
本题解:
输入一行,输出一行(求第n个丑数是谁)
#include <iostream>
#include<cstdio>
#include<string>
#include <algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<vector>
int a[2000];
typedef long long LL;
const int base[]={2,3,5};
using namespace std;
int main()
{
int n,j=1;
priority_queue<LL,vector<LL>,greater<LL> >pq;
set<LL>s;
s.insert(1);
pq.push(1);
int cnt = 0;
while(1)
{
LL x=pq.top();pq.pop();
cnt++;
if(cnt==1500)
{
break;
}
LL tmp;
for(int i=0;i<3;++i)
{
tmp=x*base[i];
if(s.count(tmp)==0)
{
s.insert(tmp);
pq.push(tmp);
}
}
}
while(cin>>n)
{ set<LL>::iterator it=s.begin();
if(n==0)
break;
for(int i=1;i<n;i++)
{
it++;
}
cout<<*it<<endl;
}
return 0;
}