# [pat题解]1005.Spell It Right(map+reduce)

pat 1005.spell it right.

python代码：

```#!/usr/bin/env python
#-*- coding: utf-8 -*-

from functools import reduce

dic = {'1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five',
'6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', '0': 'zero'}

if __name__ == '__main__':
s = input()
sum_digits = reduce(lambda x, y: int(x) + int(y), s)

sum_digits = str(sum_digits)
e_digits = list(map(lambda x: dic[x], sum_digits))

res = ''
for i in range(len(e_digits)):
res += ' '
res += e_digits[i]
print(res[1:])
```

c++代码：

```#include <iostream>
#include <cmath>
#include <stdio.h>
#include <climits>
#include <string.h>
#include <string>

using namespace std;

string dict[] = { "zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine" };

int main()
{
string snum;
cin >> snum;

int len = snum.length();
int sum = 0;
for (int i = 0; i < len; ++i)
{
sum += (snum[i] - '0');
}

if (sum == 0)
{
cout << "zero" << endl;
return 0;
}

string res = "";
while (sum != 0)
{
res = ' ' + res;
res = dict[sum % 10] + res;
sum /= 10;
}

res.pop_back();

cout << res << endl;
return 0;
}
```