Q10.7

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;

int findkth(int k)
{
	if (k == 1)
		return 0;
	if(k == 2)
		return 3;
	if(k == 3)
		return 5;
	if(k == 4)
		return 7;
	queue<int> a, b, c;
	a.push(3); a.push(5); a.push(7);
	b.push(3); b.push(5); b.push(7);
	c.push(3); c.push(5); c.push(7);
	
	int re, n = 4;
	while(n < k)
	{
		int t1 = a.front() * 3, t2 = b.front() * 5, t3 = c.front() * 7;
		
		if(t1 < t2)
		{
			if(t1 < t3)
			{
				a.pop();
				a.push(t1);
				re = t1;
			}
			else if(t1 == t3)
			{
				a.pop(); b.pop();
				a.push(t1);
				re = t1;
			}
			else
			{
				c.pop();
				c.push(t3);
				re = t3;
			}	
		}
		else if(t1 == t2)
		{
			a.pop(); b.pop();
			if(t1 == t3)
				c.pop();
			a.push(t1);
			re = t1;
		}
		else
		{
			if(t2 < t3)
			{
				b.pop();
				b.push(t2);
				re= t2;
			}
			else if(t2 == t3)
			{
				b.pop(); c.pop();
				b.push(t2);
				re = t2;
			}
			else
			{
				c.pop();
				c.push(t3);
				re = t3;
			}
		}
		n++;
	}
	if(n == k)
		return re;
}

int main(void)
{
	int k = 7;
	cout << findkth(k) << endl;
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值