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
题型分类:字符串处理
题目大意:输入一串数字,用英文单词输出这串数字的和。
解题思路:用string接受输入,再用string存储结果,每一位输出即可。
#include <string>
#include <iostream>
using namespace std;
string word[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main(int argc, char** argv) {
string input;
cin >> input;
int sum = 0;
for(int i = 0; i < input.length(); i++){
sum += input[i] - '0';
}
string output = to_string(sum);
for(int i = 0; i < output.length(); i++){
if(i != 0) cout << " ";
cout << word[output[i] - '0'];
}
return 0;
}
本篇博客详细介绍了如何解决一道编程题:输入一个非负整数N,计算并用英文单词输出其各位数字之和。通过使用string类型接收输入,计算数字总和,并将每个数字转换成英文单词输出,实现了题目的要求。
1154

被折叠的 条评论
为什么被折叠?



