`

【水题+字符串】杭电 hdu 1228 A + B

    博客分类:
  • ACM
阅读更多

/* THE PROGRAM IS MADE BY PYY */
/*----------------------------------------------------------------------------//
    Copyright (c) 2012 panyanyany All rights reserved.

    URL   : http://acm.hdu.edu.cn/showproblem.php?pid=1228
    Name  : 1228 A + B

    Date  : Wednesday, February 08, 2012
    Time Stage : half an hour

    Result:                                                                                                                                                                                                                                                                                                                                              
5328538	2012-02-08 11:53:44	Accepted	1228
0MS	152K	2470 B
C++	pyy


Test Data :

Review :
偶尔做一做简单题~~要注意zero zero + zero zero = 的情况
//----------------------------------------------------------------------------*/

#include <cstdio>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <algorithm>
using namespace std ;

#define MEM(a, v)		memset (a, v, sizeof (a))	// a for address, v for value
#define max(x, y)		((x) > (y) ? (x) : (y))
#define min(x, y)		((x) < (y) ? (x) : (y))

#define INF		(0x3f3f3f3f)
#define MAXN	(103)
#define MAXE	(MAXN*(MAXN-1)/2)

#define DEBUG	/##/

// 字符串转为数字
inline int getint (char *sz)
{
	int i = -1 ;
	if (!strcmp (sz, "zero"))
		i = 0 ;
	else if (!strcmp (sz, "one"))
		i = 1 ;
	else if (!strcmp (sz, "two"))
		i = 2 ;
	else if (!strcmp (sz, "three"))
		i = 3 ;
	else if (!strcmp (sz, "four"))
		i = 4 ;
	else if (!strcmp (sz, "five"))
		i = 5 ;
	else if (!strcmp (sz, "six"))
		i = 6 ;
	else if (!strcmp (sz, "seven"))
		i = 7 ;
	else if (!strcmp (sz, "eight"))
		i = 8 ;
	else if (!strcmp (sz, "nine"))
		i = 9 ;

	return i ;
}

void scan_two (int &a, int &b)
{
	char sz[100], num[100] ;
	int t, shift ;
	// 获得整行
	gets (sz) ;
	// get a
	a = 0 ;
	shift = 0 ;
	while (1) 
	{
		// 跳过多余的空格
		while (*(sz+shift) == ' ')
			++shift ;
		// 提取一个单词
		sscanf (sz + shift, "%s", num) ;
		// 计算位移
		shift += strlen (num) ;
		if ((t = getint(num)) == -1) // 遇到'+'或'='则跳出
			break ;
		a = a * 10 + t ;
	}
	
	b = 0 ;
	while (1)
	{
		while (*(sz+shift) == ' ')
			++shift ;
		sscanf (sz + shift, "%s", num) ;
		shift += strlen (num) ;
		if ((t = getint (num)) == -1)
			break ;
		b = b * 10 + t ;
	}
}

int main ()
{
	int a, b ;
	while (scan_two(a, b), a | b)
	{
		printf ("%d\n", a + b) ;
	}
	return 0 ;
}

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics