PAT(Advanced)甲级---1005 Spell It Right (20 分)

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 (≤10​100​​).

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

 

代码:

#include
#include
#include
#include
using namespace std;



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

vector v;

int main(){
	string num;
	cin >> num;
	int sum = 0;
	for (auto x : num)sum += x - '0';
	if (sum == 0){
		printf("zero");
		return 0;
	}
	
		//取数
	while (sum != 0){
		v.push_back(sum % 10);
		sum /= 10;
	}
	for (auto it = v.rbegin(); it != v.rend(); it++){
		if (it != v.rbegin())printf(" ");
		printf("%s", translate[*it].c_str());
	}
}

总结:

  • 注意到N的范围,考虑字符串处理

1.建立映射表
2.遍历求和,从个位开始取数(常用套路)

你可能感兴趣的:(PAT甲级,数据结构)