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

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();
    }
};
时间: 2024-10-01 22:43:07

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

PAT 1059. Prime Factors (25)

1059. Prime Factors (25) Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specification: Each input file contains one test case which gives a positive inte

1059. Prime Factors (25)

时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1* p2^k2 *…*pm^km. Input Specification: Each input file contain

2014辽宁ACM省赛 Prime Factors

问题 L: Prime Factors 时间限制: 1 Sec  内存限制: 128 MB 提交: 36  解决: 28 [提交][状态][论坛] 题目描述 I'll give you a number , please tell me how many different prime factors in this number. 输入 There is multiple test cases , in each test case there is only one line contain

PAT1059. Prime Factors

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specification: Each input file contains one test case which gives a positive integer N in the range of lon

pat1059. Prime Factors (25)

1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specificatio

(笔试题)质数因子Prime Factor

题目: Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *...*pm^km. 输入描述: Each input file contains one test case which gives a positive integer N in the range of long int. 输出

A1059. Prime Factors (25)

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specification: Each input file contains one test case which gives a positive integer N in the range of lon

uva 10780 Again Prime? No Time. 质因子乱搞

求最大的k   使得 m^k 能被n!整除 m^k就是让m的每个质因子个数增加了k倍,只要求得n!的质因子能让m增加多少倍就够了.当然这里要取增加倍数最少的. 木桶装水的量取决于最短的木板. 预处理2-n每个数的质因子情况,由于n有10000,前10000个素数有1000+个,所以维护前缀和不划算. case只有500 所以干脆每次都算一遍. #include<stdio.h> #include<string.h> #include<iostream> #include

[CareerCup] 18.4 Count Number of Two 统计数字2的个数

18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如果n=20,那么满足题意的是2, 12, 20,那么返回3即可.LeetCode上有一道很类似的题Factorial Trailing Zeroes,但是那道题求5的个数还包括了因子中的5,比如10里面也有5,这是两题的不同之处.那么首先这题可以用brute force来解,我们对区间内的每一个数字