• 剑指offer 面试题34 丑数


      题目链接: 剑指offer

      题目描述: 规定素数分解只有 2, 3, 5的数字为丑数, 输入N , 求第N个丑数

      解题思路: 一个丑数肯定是由上一个丑数得来的, 我们只需要求出根据已经存在的所有丑数, 求第N个丑数

      代码: 

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cstring>
    #include <iterator>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    #include <deque>
    #include <map>
    #include <set>
    #include <queue>
    #define lson l, m, rt<<1
    #define rson m+1, r, rt<<1|1
    #define mem0(a) memset(a,0,sizeof(a))
    #define mem1(a) memset(a,-1,sizeof(a))
    #define sca(x) scanf("%d",&x)
    #define de printf("=======
    ")
    typedef long long ll;
    using namespace std;
    
    int Get_Ugly_Number_Solution2(int index) {
        if( index < 0 ) return 0;
        int *pUglyNumbers = new int[index];
        pUglyNumbers[0] = 1;
        int nextUglyIndex = 1;
        int * pMultiply2 = pUglyNumbers;
        int * pMultiply3 = pUglyNumbers;
        int * pMultiply5 = pUglyNumbers;
        while( nextUglyIndex < index ) {
            int Min = min({*pMultiply2*2, *pMultiply3*3, *pMultiply5*5});
            pUglyNumbers[nextUglyIndex] = Min;
            while( *pMultiply2*2 <= Min ) ++pMultiply2;
            while( *pMultiply3*3 <= Min ) ++pMultiply3;
            while( *pMultiply5*5 <= Min ) ++pMultiply5;
            ++nextUglyIndex;
        }
        int ugly = pUglyNumbers[index-1];
        delete [] pUglyNumbers;
        return ugly;
    }
    
    int main() {
        int index;
        scanf( "%d", &index );
        printf( "%d
    ", Get_Ugly_Number_Solution2(index));
        return 0;
    //
    //    stack<int> temp_stack;
    //    for( int i = 0; i < 12; i++ ) {
    //        temp_stack.push(i);
    //    }
    //    while( !temp_stack.empty() ) {
    //        cout << temp_stack.top() << endl;
    //        temp_stack.pop();
    //    }
    }
    View Code

      思考: 这个没啥吧......需要记得看看是不是能够由上一个状态进行转移, 还有一个就是这个数本身的特点, 比如二进制或者能不能划分成子问题等等

  • 相关阅读:
    docker学习笔记
    无法启动此程序,因为计算机中丢失api-ms-win
    pandas, groupby
    Chapter Five, More Than Two Variables: Graphical Multivariate Analysis
    ARIMA, Autoregressive Moving Average
    Chapter Four, Time As a Variable: Time-Series Analysis
    Exponentially Weighted Moving-Average
    KaKs_calculator
    pal2nal
    clustal
  • 原文地址:https://www.cnblogs.com/FriskyPuppy/p/7517981.html
Copyright © 2020-2023  润新知