POJ 2247 Humble Numbers

题目大意:

        因子只含2, 3, 5或7的数称为丑数(1是特殊的丑数),将丑数从小到大排列得到序列1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27....

        现要求编程求出序列中第n个丑数。

        现有多个测例,每个测例都给出n(测例以n=0表示结束),要求输出"The nth humble number is number xxx.",其中n的后缀可以为st, nd, rd或th,要根据实际情况决定(十位数字为1的都为th,其余情况下看各位数字,各位数字1, 2, 3分别对应st, nd, rd,其余也为th)。

题目链接

注释代码:

/*                    
 * Problem ID : POJ 2247 Humble Numbers
 * Author     : Lirx.t.Una                    
 * Language   : C          
 * Run Time   : 16 ms                    
 * Run Memory : 180 KB                    
*/   

#include <stdio.h>

//maximum number of humble numbers
//丑数的最大个数
#define	MAXHN		5842

#define	MIN(x,y)		( (x) < (y) ? (x) : (y) )
#define	MIN4(a,b,c,d)	MIN( MIN( (a), (b) ), MIN( (c), (d) ) )

int		h[MAXHN + 1];//丑数序列

int
main() {

	int		n;//表示要求的第几个丑数
	int		c2, c3, c5, c7;//质因数2, 3, 5, 7的计数器,用于打表
	int		v2, v3, v5, v7;//表示质因数含2, 3, 5, 7的当前丑数

	int		ten;//数字的十位部分

	int		tmp;//临时变量

	//都先从下标1开始计,下标1存放丑数1
	c2 = 1;
	c3 = 1;
	c5 = 1;
	c7 = 1;

	h[1] = 1;//下标1存放丑数1
	n    = 2;//这里n作为打表的计数器,表示当前指针

	while ( n <= MAXHN ) {
	
		//获得当前丑数
		v2 = 2 * h[c2];
		v3 = 3 * h[c3];
		v5 = 5 * h[c5];
		v7 = 7 * h[c7];

		//选取其中最小丑数放在当前序列中,这样可以保证丑数序列是递增的
		tmp    = MIN4( v2, v3, v5, v7 );
		h[n++] = tmp;

		//选取命中的指针递增
		//注意!!肯能有多个值等于tmp
		//因此这里不能用continue作为分句,否则在后面的计数中会重复
		if ( tmp == v2 ) c2++;
		if ( tmp == v3 ) c3++;
		if ( tmp == v5 ) c5++;
		if ( tmp == v7 ) c7++;
	}

	while ( scanf("%d", &n), n ) {
		
		printf("The %d", n);
		
		ten = n / 10 % 10;//决定后缀
		if ( ten != 1 )
			switch ( n % 10 ) {
			
				case 1  : printf("st "); break;
				case 2  : printf("nd "); break;
				case 3  : printf("rd "); break;
				default : printf("th "); break;
		}
		else
			printf("th ");
		
		printf("humble number is %d.\n", h[n]);//查表输出
	}
	
	return 0;
}

无注释代码:

#include <stdio.h>

#define	MAXHN		5842

#define	MIN(x,y)		( (x) < (y) ? (x) : (y) )
#define	MIN4(a,b,c,d)	MIN( MIN( (a), (b) ), MIN( (c), (d) ) )

int		h[MAXHN + 1];

int
main() {

	int		n;
	int		c2, c3, c5, c7;
	int		v2, v3, v5, v7;

	int		ten;

	int		tmp;

	c2 = 1;
	c3 = 1;
	c5 = 1;
	c7 = 1;

	h[1] = 1;
	n    = 2;

	while ( n <= MAXHN ) {
		
		v2 = 2 * h[c2];
		v3 = 3 * h[c3];
		v5 = 5 * h[c5];
		v7 = 7 * h[c7];
		
		tmp    = MIN4( v2, v3, v5, v7 );
		h[n++] = tmp;
		
		if ( tmp == v2 ) c2++;
		if ( tmp == v3 ) c3++;
		if ( tmp == v5 ) c5++;
		if ( tmp == v7 ) c7++;
	}
	
	while ( scanf("%d", &n), n ) {
		
		printf("The %d", n);
		
		ten = n / 10 % 10;
		if ( ten != 1 )
			switch ( n % 10 ) {
			
				case 1  : printf("st "); break;
				case 2  : printf("nd "); break;
				case 3  : printf("rd "); break;
				default : printf("th "); break;
		}
		else
			printf("th ");
		
		printf("humble number is %d.\n", h[n]);
	}
	
	return 0;
}

单词解释:

humble:adj, 谦虚的,简陋的

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值