面试题 34 丑数

int minthree(int a1, int a2, int a3){

	a1 = a1 < a2 ? a1 : a2;
	a1 = a1 < a3 ? a1 : a3;
	return a1;
	
}
int getugly(int index){

	queue<int> q2, q3, q5;
	int res = 1;
	q2.push(2);
	q3.push(3);
	q5.push(5);
	while(index > 1){
		--index;
		int tp = minthree(q2.front(), q3.front(), q5.front());
		if(tp == q2.front()){
			q2.pop();
			q2.push(tp*2);
			q3.push(tp*3);
			q5.push(tp*5);
		}else if(tp == q3.front()){
			q3.pop();
			q3.push(tp*3);
			q5.push(tp*5);
		}else{
			q5.pop();
			q5.push(tp*5);
		}
		res = tp;
	}

	return res;
}

  

posted @ 2013-09-16 16:22  冰点猎手  阅读(208)  评论(0编辑  收藏  举报