糯米

TI DaVinci, gstreamer, ffmpeg
随笔 - 167, 文章 - 0, 评论 - 47, 引用 - 0
数据加载中……

POJ 1338 Ugly Numbers 水题

思路:
事先计算出第1500个数为859963392,枚举2,3,5的所有乘积,过滤掉所有大于859963392的数。
积攒到1500个的时候停止计算。然后快排就行了。速度还挺快呢。0ms。

#include <stdio.h>
#include 
<stdlib.h>

#define MAX_N 859963392

__int64 arr[
1501];
int cnt;

int cmp(const void *a, const void *b)
{
    
return *(__int64 *)a - *(__int64 *)b;
}


int main()
{
    __int64 a, b, c;
    
int i;

    
/*
    cnt = 0;
    for (i = 1; cnt < 1500; i++) {
        val = i;
        while (!(val & 1))
            val >>= 1;
        while (!(val % 3))
            val /= 3;
        while (!(val % 5))
            val /= 5;
        if (val == 1) {
            printf("#%d %d\n", cnt, i);
            cnt++;
        }
    }
    
*/

    
for (a = 1; a <= MAX_N; a *= 2{
        
for (b = 1; a*<= MAX_N; b *= 3{
            
for (c = 1; a*b*<= MAX_N; c *= 5{
                arr[
++cnt] = a*b*c;
                
if (cnt >= 1500)
                    
goto done;
            }

        }

    }


done:
    qsort(arr 
+ 11500sizeof(arr[0]), cmp);

    
while (scanf("%d"&i), i)
        printf(
"%d\n", arr[i]);

    
return 0;
}

posted on 2010-03-01 14:34 糯米 阅读(330) 评论(0)  编辑 收藏 引用 所属分类: POJ


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理