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

UVa 11621 - Small Factors

时间:2015-07-17 17:39:16      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

称号:发现没有比给定数量少n的。只要2,3一个因素的数字组成。

分析:数论。贪婪,分而治之。

            用两个三分球,分别代表乘法2,和繁殖3队列,队列产生的数字,原来{1}。

            然后。每取两个指针相应元素*2和*3的值中最小的即为未找到的数字中最小的;

            注意,可能生成反复数据。不要存进去(反复数据。一定连续产生)。

说明:打表计算。二分查询输出就可以。

#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

int next[330];

int bs(int key, int r)
{
	int l = 0,m;
	while (l < r) {
		m = (l+r)/2;
		if (next[m] < key)
			l = m+1;
		else r = m;
	}
	return r;
}

int main()
{
	int two = 0,three = 0,count = 0;
	next[0] = 1;
	while (next[count] > next[count-1]) {
		if (next[two]*2 < next[three]*3) 
			next[++ count] = next[two ++]*2;
		else {
			if (next[three]*3 == next[two]*2) two ++;
			next[++ count] = next[three ++]*3;
		}
	}
	
	int n;
	while (cin >> n && n)
		cout << next[bs(n, count)] << endl;
		
	return 0;
}


版权声明:本文博客原创文章。博客,未经同意,不得转载。

UVa 11621 - Small Factors

标签:

原文地址:http://www.cnblogs.com/yxwkf/p/4654688.html

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