您的位置:首页 > 其它

UVa 136 - Ugly Numbers

2013-07-15 23:51 435 查看
  题目大意:只有素因子2,3,5的数叫做丑数。输出第1500个丑数即可。

  这个...好吧,直接输出就是了。自己写一个小程序先计算一下,这就是黑盒测试的好处啊,“我们的目标是解决问题,而不是为了写程序而写程序,同时应该保持简单(Kepp It Simple and Stupid, KISS)”,摘自《算法竞赛入门经典》。

#include <cstdio>

int main()
{
int p = 1; // the number of ugly number
int i;
for (i = 2; ; i++)
{
int t = i;
while (t % 2 == 0)   t /= 2;
while (t % 3 == 0)   t /= 3;
while (t % 5 == 0)   t /= 5;
if (t == 1)
{
p++;
//printf("%d is the %dth ugly number\n", i, p);
}
if (p >= 1500)   break;
}
printf("%d\n", i);
return 0;
}


View Code
  这个是暴力枚举测试的,简单直接,不过效率不高,还看到一个用dp解决的,以后在完善啦

  忽然想试一下这道题c和c++的差别,就换了一下头文件用c提交,竟然用了12ms,c++才用了9ms,为什么会这样呢?c不是应该比c++快的吗? 2013.7.16
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: