zoukankan      html  css  js  c++  java
  • 九度OJ 1214 寻找丑数【算法】

    题目地址:http://ac.jobdu.com/problem.php?pid=1214

    题目描述:

    把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。
    习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。

    输入:

    输入包括一个整数N(1<=N<=1500)。

    输出:

    可能有多组测试数据,对于每组数据,
    输出第N个丑数。

    样例输入:
    3
    样例输出:
    3
    #include <stdio.h>
     
    int min (int num1, int num2, int num3){
        int min = (num1 < num2) ? num1 : num2;
        return min = (min < num3) ? min : num3;
    }
     
    void GetUglyNumbers (int UglyNumbers[], int index){
        if (index < 1)
            return;
     
        int T2 = 0;
        int T3 = 0;
        int T5 = 0;
        int nextUglyIndex = 1;
     
        UglyNumbers[0] = 1;
        while (nextUglyIndex < index){
            UglyNumbers[nextUglyIndex] = min (UglyNumbers[T2] * 2, UglyNumbers[T3] * 3, UglyNumbers[T5] * 5);
            while (UglyNumbers[T2] * 2 <= UglyNumbers[nextUglyIndex])
                ++T2;
            while (UglyNumbers[T3] * 3 <= UglyNumbers[nextUglyIndex])
                ++T3;
            while (UglyNumbers[T5] * 5 <= UglyNumbers[nextUglyIndex])
                ++T5;
            ++nextUglyIndex;
        }
    }
     
    int main (void){
        int UglyNumbers[1500];
        int n;
     
        GetUglyNumbers (UglyNumbers, 1500);
        while (scanf ("%d", &n) != EOF){
            printf ("%d
    ", UglyNumbers[n - 1]);
        }
     
        return 0;
    }

    参考资料:何海涛 -- 程序员面试题精选100题(37)-寻找丑数[算法]

  • 相关阅读:
    做前端的一些小工具
    分析几种编程语言对JSON的支持程度
    注册中心eureka
    搭建分布式配置中心
    接口幂等性
    分布式限流
    服务容错解决方案
    微服务架构认知
    gateWay
    JWT鉴权
  • 原文地址:https://www.cnblogs.com/liushaobo/p/4373799.html
Copyright © 2011-2022 走看看