2 ^ x * 3 ^ y * 5 ^ z的线性解

时间:2016-06-22 18:19:25

标签: c++

我需要在升序中生成1,2,3,4,5,6,8,9,10,12,15,16等(给定的n个数字)(仅包含2,3和5的幂)以线性时间(O(n))排序。

我有以下代码(我不太确定它的复杂性,也会很感激其复杂性的答案)

bool s_a (int a)
{
    while (a % 2 == 0)
        a = a / 2;
    while (a % 3 == 0)
        a = a / 3;
    while (a % 5 == 0)
        a = a / 5;
    if (a == 1)
        return true;
    else
        return false;
}
void s_b (int n)
{
    unsigned int k = 1,nc = 1;
    while ( k <= n)
    {
        if(s_a(nc) == true)
        {
            cout << nc << " ";
            k++;
        }
        nc++;
    }
}

0 个答案:

没有答案