点点滴滴

发现不足,弥补不足,持续的努力~

1005. Spell It Right (20)

1005. Spell It Right (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:
12345
Sample Output:

one five

//1005. Spell It Right (20)
string ChangTostr(int n)//将数字对应到字符串
{
	switch (n)
	{
	case 0:return "zero"; break;
	case 1:return "one"; break;
	case 2:return "two"; break;
	case 3:return "three"; break;
	case 4:return "four"; break;
	case 5:return "five"; break;
	case 6:return "six"; break;
	case 7:return "seven"; break;
	case 8:return "eight"; break;
	case 9:return "nine"; break;
	}
}

int main()
{
	string s;
	cin >> s;
	int sum = 0;
	for (int i = 0; i < s.length(); i++)
	{
		int num = s[i] - '0';
		sum += num;//计算和
	}
	stringstream ss;
	ss << sum;
	string str = ss.str();//将整型转为字符串,方便理解
	for (int i = 0; i < str.length(); i++)
	{
		int num = str[i] - '0';
		if (i)
		{
			cout << " " << ChangTostr(num);
		}			
		else
		{
			cout << ChangTostr(num);
		}
	}
	return 0;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fynjy/article/details/46778047
个人分类: Advanced PAT C++
上一篇1002. A+B for Polynomials (25)
下一篇1008. Elevator (20)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭