题意:给出三个质数,求这素因子只有这三个质数的数中第k大的。
分析:用一个数列,第一位是1。用三个指针指向三个prime要乘的被乘数,最开始都指向1。每次取乘积最小的加入数组,并把指针后移。加入时要判断是否重复,若重复则不加入。
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
usingnamespace std;
#define maxn 10000
longlong p[3], n, pos[3], a[maxn];
int main()
{
//freopen("t.txt", "r", stdin);
scanf("%lld%lld%lld%lld", &p[0], &p[1], &p[2], &n);
pos[0] = pos[1] = pos[2] =0;
a[0] =1;
longlong count =1;
while (count <= n)
{
longlong best, besti;
best = a[pos[0]] * p[0];
besti =0;
if (a[pos[1]] * p[1] < best)
{
best = a[pos[1]] * p[1];
besti =1;
}
if (a[pos[2]] * p[2] < best)
{
best = a[pos[2]] * p[2];
besti =2;
}
if (best != a[count -1])
a[count++] = best;
pos[besti]++;
}
printf("%lld\n", a[n]);
return0;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
usingnamespace std;
#define maxn 10000
longlong p[3], n, pos[3], a[maxn];
int main()
{
//freopen("t.txt", "r", stdin);
scanf("%lld%lld%lld%lld", &p[0], &p[1], &p[2], &n);
pos[0] = pos[1] = pos[2] =0;
a[0] =1;
longlong count =1;
while (count <= n)
{
longlong best, besti;
best = a[pos[0]] * p[0];
besti =0;
if (a[pos[1]] * p[1] < best)
{
best = a[pos[1]] * p[1];
besti =1;
}
if (a[pos[2]] * p[2] < best)
{
best = a[pos[2]] * p[2];
besti =2;
}
if (best != a[count -1])
a[count++] = best;
pos[besti]++;
}
printf("%lld\n", a[n]);
return0;
}