HDOJ 1058 Humble Numbers

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

static int hnums[5842+1]={0,1};
static void humble_number()
{
	static int pnums[8]={0};
	pnums[2]=1;
	pnums[3]=1;
	pnums[5]=1;
	pnums[7]=1;
	int prime_factors[4]={2,3,5,7};
	int i,j;
	for(i=1;i<5843;i++){
		int min=INT_MAX;
		int k;
		for(k=0;k<4;k++){
			for(j=pnums[prime_factors[k]];j<=i;j++){
				if(hnums[j]*prime_factors[k]>hnums[i]){
					pnums[prime_factors[k]]=j;
					if(hnums[j]*prime_factors[k]<min)
						min=hnums[j]*prime_factors[k];
					break;
				}
			}
		}
		hnums[i+1]=min;
	}
}

int main(int argc,char *argv[])
{
	humble_number();
	const char * symv[4];

	symv[0]="th";

	symv[1]="st";
	symv[2]="nd";
	symv[3]="rd";
	int n;
	while(scanf("%d",&n),n){
		int one_bit,two_bit;
		one_bit=n%10;
		if(one_bit>0 && one_bit<4){
			two_bit=n%100;
			if(two_bit!=11 && two_bit!=12 && two_bit!=13){
				printf("The %d%s humble number is %d.\n",n,symv[one_bit],hnums[n]);
				continue;
			}
		}
		printf("The %d%s humble number is %d.\n",n,symv[0],hnums[n]);
	}
	return 0;
}


 

你可能感兴趣的:(include,Numbers)