码迷,mamicode.com
首页 > 其他好文 > 详细

[CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字

时间:2015-09-03 23:26:06      阅读:466      评论:0      收藏:0      [点我收藏+]

标签:

 

7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7.

 

这道题跟之前LeetCode的那道Ugly Number II 丑陋数之二基本没有啥区别,具体讲解可参见那篇,代码如下:

 

class Solution {
public:
    int getKthMagicNumber(int k) {
        vector<int> res(1, 1);
        int i3 = 0, i5 = 0, i7 = 0;
        while (res.size() < k) {
            int m3 = res[i3] * 3, m5 = res[i5] * 5, m7 = res[i7] * 7;
            int mn = min(m3, min(m5, m7));
            if (mn == m3) ++i3;
            if (mn == m5) ++i5;
            if (mn == m7) ++i7;
            res.push_back(mn);
        }
        return res.back();
    }
};

 

[CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字

标签:

原文地址:http://www.cnblogs.com/grandyang/p/4780868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!