PersonalCorpus 版 (精华区)

Your C++ program has solved Ok the problem 443 (Humble Numbers)
in 0.060 seconds with low memory spent.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
#include <ctype.h>

int cmp_int(const void *a, const void *b)
{
    if (*(int *)a>*(int *)b) return 1;
    if (*(int *)a==*(int *)b) return 0; else return -1;
}

int cmp_double(const void *a, const void *b)
{
    if (*(double *)a>*(double *)b) return 1;
    if (*(double *)a==*(double *)b) return 0; else return -1;
}

void main()
{
#ifndef ONLINE_JUDGE
    freopen("c:\\uva\\input443.txt","r",stdin);
    freopen("c:\\uva\\output443.txt","w",stdout);
#endif

    double s[6000],a,b,c,d;

    int e=0,i,j,k;

    for (a=1;a<=2000000000;a*=2) //Special thanks to Darcy!!
    for (b=a;b<=2000000000;b*=3)
    for (c=b;c<=2000000000;c*=5)
    for (d=c;d<=2000000000;d*=7) s[e++]=d;

    qsort((void *)s,5842,sizeof(double),cmp_double);

    scanf("%d\n",&i);
    while(i!=0) {
        j=i%100;
        if ((j>10)&&(j<20)) {
            printf("The %dth humble number is %.0lf.\n",i,s[i-1]);
            scanf("%d\n",&i);
            continue;
        }
        j=i%10;
        switch(j) {
            case 1:printf("The %dst humble number is %.0lf.\n",i,
s[i-1]);break;
            case 2:printf("The %dnd humble number is %.0lf.\n",i,
s[i-1]);break;
            case 3:printf("The %drd humble number is %.0lf.\n",i,
s[i-1]);break;
            default:printf("The %dth humble number is %.0lf.\n",i,
s[i-1]);break;
        }
        scanf("%d\n",&i);
    }
}
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.577毫秒