UVa 136

  题目大意:只有素因子2,3,5的数叫做丑数。输出第1500个丑数即可。

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

 1 #include <cstdio>
 2 
 3 int main()
 4 {
 5     int p = 1; // the number of ugly number
 6     int i;
 7     for (i = 2; ; i++)
 8     {
 9         int t = i;
10         while (t % 2 == 0)   t /= 2;
11         while (t % 3 == 0)   t /= 3;
12         while (t % 5 == 0)   t /= 5;
13         if (t == 1)
14         {
15             p++;
16             //printf("%d is the %dth ugly number
", i, p);
17         }
18         if (p >= 1500)   break;
19     }
20     printf("%d
", i);
21     return 0;
22 }
View Code

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


  忽然想试一下这道题c和c++的差别,就换了一下头文件用c提交,竟然用了12ms,c++才用了9ms,为什么会这样呢?c不是应该比c++快的吗?        2013.7.16

原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3192454.html