丑数运算 一、((输出丑数n的下标)(给定丑数输下标)) 二、((求第n个丑数是谁)(给定下标求丑数))

丑数运算:

先说一下解题的部分知识点:

迭代器中元素距离关系:

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值