NPOJ 1063 火柴棒拼数

本文提供了一道名为“喵星人吃土豆”的编程题的解决方案,使用C语言实现。该题需要计算输入数字用火柴棒表示所需的火柴棒数量,特别处理了前导零的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:中文

注释代码:

/*                                                    
 * Problem ID : NPOJ 1065 喵星人吃土豆
 * Author     : Lirx.t.Una                                                    
 * Language   : C                                  
 * Run Time   : 14                                                    
 * Run Memory : 1092                                                    
*/ 

#include <stdio.h>

//数字的最大位数(最后一位留给'\0')
#define	MAXLEN		1000

//num[i]表示数字i对应的火柴棒的数量
char	num[10] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
char	sx[MAXLEN];//string x,表示数字x的字符串

int
main() {
	
	int		t;//测例数
	int		iscn;
	
	int		ans;//最终火柴棒的数量
	
	int		i;//计数变量
	
	char *	x;//偏移指针
	
	scanf("%d", &t);
	iscn = 0;
	
	while ( t-- ) {
		
		scanf("%s", sx);
		x = sx;
		
		printf("Case #%d: ", ++iscn);
		
		while ( *x && '0' == *x ) x++;//忽略前导0
		if ( !*x ) { puts("6"); continue; }//如果直接是0则输出0的火柴棒数6
		
		ans = 0;
		while ( *x ) ans += (int)num[ *(x++) - '0' ];//否则就计算前导0后面的火柴棒书
		printf("%d\n", ans);
	}
	
	return 0;
}
无注释代码:

#include <stdio.h>

#define	MAXLEN		1000

char	num[10] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
char	sx[MAXLEN];

int
main() {

	int		t;
	int		iscn;

	int		ans;

	int		i;

	char *	x;

	scanf("%d", &t);
	iscn = 0;

	while ( t-- ) {
	
		scanf("%s", sx);
		x = sx;

		printf("Case #%d: ", ++iscn);

		while ( *x && '0' == *x ) x++;
		if ( !*x ) { puts("6"); continue; }

		ans = 0;
		while ( *x ) ans += (int)num[ *(x++) - '0' ];
		printf("%d\n", ans);
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值