Spell It Right

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

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

题目解析

  本题给出一个小于10的100次方的数字,要求计算其每一位的数字的和,并由最大为开始以英文输出和的每一位。

  将给出的数字记录在字符串num中计算字符串每一位减去字符’0’后的和即可得到给出数字每一位的和,以字符串数组str[ ]记录其下标数字对应的英文单词。

  最后将得到的和每一位对应的单词记录并输出即可。

 #include <bits/stdc++.h>
using namespace std;
const string str[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int getSum(string num){ //计算给出数字每一位的和
int ans = ;
for(auto i : num)
ans += i - '';
return ans;
}
int main()
{
string num;
cin >> num; //输入num
int sum = getSum(num); //求每一位的和
vector<string> ans; //记录sum每一位对应的英文单词
do{ //由于可能出现sum为0的情况,所以用do while
ans.push_back(str[sum % ]);
sum /= ;
}while(sum);
reverse(ans.begin(), ans.end()); //由于记录时时由低位到高位记录,所以将其反转ans
for(int i = ; i < ans.size(); i++){ //输出答案
if(i != )
printf(" ");
cout << ans[i];
}
return ;
}

最新文章

  1. sqlserver索引小结
  2. Objective-C instancetype关键字
  3. Update-Package : Unable to load the service index for source https://api.nuget.org/v3/index.json.
  4. Mac配置
  5. [转]ldconfig几个需要注意的地方
  6. 初识HTTP 1.1与HTTP 1.0
  7. 《python基础教程》笔记之 更加抽象
  8. Spark里边:Worker源代码分析和架构
  9. Python装饰器的解包装(unwrap)
  10. phpmailer发送邮件服务
  11. python enumerate() 函数的使用方法
  12. WEB前端常见面试题汇总:(一)
  13. Mysql 视图,触发器,存储过程,函数,事务
  14. RPM包的版本号比较
  15. what&#39;s the 颈线
  16. 图的 储存 深度优先(DFS)广度优先(BFS)遍历
  17. mvc中文件上传下载
  18. day36 数据库表操作 数据类型 完整性约束
  19. Java常用工具类之ArrayUtil
  20. Hibernate系列之ID生成策略

热门文章

  1. Android listview 侧滑 SwipeListView 详解 实现微信,QQ等滑动删除效果
  2. sublime text配置node.js调试
  3. G - Christmas Play
  4. hdu 1.2.7
  5. 等到花儿也谢了的await
  6. OpencvSharp 在WPF的Image控件中显示图像
  7. 也来谈一谈js的浅复制和深复制
  8. UCore-Lab0
  9. spring cloud学习(五) 配置中心
  10. 跟着刚哥学习Spring框架--事务配置(七)