2015年8月20日20:25:45 丑数IsUgly

<pre name="code" class="cpp">bool IsUgly(int number)
{
	while(number%2==0)	number/=2;
	while(number%3==0)	number/=3;
	while(number%5==0)	number/=5;
	
	return (number ==1 ) ?true:false;
}

//o(n^2)
int GetUglyNumber_Solution2(int index)
{
	if(index<=0) return 0;
	
	int *pUglyNumbers =new int [index];
	pUglyNumbers[0];
	int nextUglyIndex=1;
	
	int *pMultiply2 = pUglyNumbers;
	int *pMultiply3 = pUglyNumbers;
	int *pMultiply5 = pUglyNumbers;
	while(nextUglyIndex<index)
	{
		int min =min(*pMultiply2*2,*pMultiply3*3,*pMultiply5*5);
		pUglyNumbers[nextUglyIndex]=min;
		
		while(*pMultiply2*2<=pMultiply[nextUglyIndex])
			++pMultiply2;
		while(*pMultiply3*3<=pMultiply[nextUglyIndex])
			++pMultiply3;
		while(*pMultiply5*5<=pMultiply[nextUglyIndex])
			++pMultiply5;
			
			++nextUglyIndex;
	}
	int ugly =pUglyNumbers[nextUglyIndex-1];
	delete[] pUglyNumbers;a
	return ugly;

}

int Min(int number1,int number2,int number3)
{
	int min=(number1<number2)?number1:number2;
	min=(min<number3)?min:number3;

	return min;
}

//1500 UglyNumber Arryay,consume 6KB memery.
//<o(n^2)



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值