读书人

某博客看到的面试题,帮忙看看是小弟我

发布时间: 2013-06-26 14:29:32 作者: rapoo

某博客看到的面试题,帮忙看看是我理解错了,还是程序错了
我们把只包含因子2、3和5的数称作丑数(Ugly Number)。
例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。

#include <stdio.h>

int first_chk(int no)
{
if(no%2==0||
no%3==0||
no%5==0)
return 0;
return -1;
}
int second_chk(int no)
{
if(no%4==0||
no%6==0||
no%7==0||
no%8==0||
no%9==0)
return -1;
return 0;
}
main()
{
int i=0;
int count=1;
int array[2013];

for(i=2;i<1000000000;i++)
{
if(2012==count)
break;
if(!first_chk(i))
if(!second_chk(i))
{
array[count]=i;
printf("the [%d]-->%d\n",count,array[count]);
count++;
}
}
return;
}


大家帮忙多多指点! C
[解决办法]
你这算法有问题吧。在看看那算法。
[解决办法]
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;

}

读书人网 >UNIXLINUX

热点推荐