zoukankan      html  css  js  c++  java
  • 【UVA

    Ugly Numbers

    Descriptions:

    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, 15, ...
    shows the first 11 ugly numbers. By convention, 1 is included.
    Write a program to find and print the 1500’th ugly number.

    Input

    There is no input to this program

    Output

    Output should consist of a single line as shown below, with ‘’ replaced by the number computed. 

    Sample Output

    The 1500'th ugly number is <number>.
    题意
    丑数是指不能被2,3,5以外的其他素数整除的数。把丑数从小到大排列起来,结果如下:
    1,2,3,4,5,6,8,9,10,12,15……
    求第1500个丑数

    输入
    没有输入
    输出
    The 1500'th ugly number is <number>.

    题目链接:

    https://vjudge.net/problem/UVA-136

    不难发现2,3,5之后的丑数全是有这三个数乘上{2,3,5}之后得来的,这就好办了,因为不可重复,还需要按顺序来,所以选择<set>是没有问题的,用<set>存入这些丑数,记录 it 的大小,当it为1500-1(数组从0开始计数)时就是我们要找的这个丑数

    AC代码:

    #include <iostream>
    #include <cstdio>
    #include <fstream>
    #include <algorithm>
    #include <cmath>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <cstring>
    #include <map>
    #include <stack>
    #include <set>
    #include <sstream>
    #define mod 1000000007
    #define ll long long
    #define INF 0x3f3f3f3f
    #define ME0(x) memset(x,0,sizeof(x))
    using namespace std;
    set<ll> num;//存入丑数
    set<ll>::iterator it;
    int main()
    {
        int a[3]= {2,3,5};
        num.insert(1);
        int sum=0;//这里面肯定会有重复的,所以要重新设一个计数器
        it=num.begin();//从0开始计数
        while(sum<1500-1)//当然sum的值可以更大一点,这也没事,主要是去重复
        {
            for(int i=0; i<3; i++)
                num.insert(*it * a[i]);
            it++;//记录当前为第几个丑数
            sum++;
        }
        printf("The 1500'th ugly number is %llu.
    ", *it);
    }







    
    
  • 相关阅读:
    基于redis的分布式锁实现方案
    nginx 配置文件详解
    Linux 下安装Nginx
    Nginx简介
    Spring Boot 整合 Spring Security 示例实现前后分离权限注解 + JWT 登录认证
    idea2020.1版本下载安装与激活
    Cloud Alibaba --Nacos
    spring Cloud Alibaba 简介
    Eclipse中SVN更改连接用户
    Win7(64bit)搭建SVN
  • 原文地址:https://www.cnblogs.com/sky-stars/p/10994824.html
Copyright © 2011-2022 走看看