题目大意:只有素因子2,3,5的数叫做丑数。输出第1500个丑数即可。
这个...好吧,直接输出就是了。自己写一个小程序先计算一下,这就是黑盒测试的好处啊,“我们的目标是解决问题,而不是为了写程序而写程序,同时应该保持简单(Kepp It Simple and Stupid, KISS)”,摘自《算法竞赛入门经典》。
#include <cstdio> int main()
{
int p = ; // the number of ugly number
int i;
for (i = ; ; i++)
{
int t = i;
while (t % == ) t /= ;
while (t % == ) t /= ;
while (t % == ) t /= ;
if (t == )
{
p++;
//printf("%d is the %dth ugly number\n", i, p);
}
if (p >= ) break;
}
printf("%d\n", i);
return ;
}
这个是暴力枚举测试的,简单直接,不过效率不高,还看到一个用dp解决的,以后在完善啦
忽然想试一下这道题c和c++的差别,就换了一下头文件用c提交,竟然用了12ms,c++才用了9ms,为什么会这样呢?c不是应该比c++快的吗? 2013.7.16