栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

1005 Spell It Right (20 分)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

1005 Spell It Right (20 分)

题目

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

思路

本题较为简单,只需知道
1,字符怎么转换为int即可,直接用int类型强转,得到字符ascii,再-48即是对应的数字。
2,数字怎么转换为字符串to_string(int a),方便得到求和结果的每一数位。当然也可以用求余的方法得出。
3,扩展——字符串怎么转换为数字atoi(s.c_str()),其中s为字符串。
这一题和1001题其实差不多,就是多了一个mapstl库的使用
PAT甲级题库 1001 A+B Format (20 分)

题解
#include
#include
#include
using namespace std;
void inio(map& a)
{
	a[0] = "zero";
	a[1] = "one";
	a[2] = "two";
	a[3] = "three";
	a[4] = "four";
	a[5] = "five";
	a[6] = "six";
	a[7] = "seven";
	a[8] = "eight";
	a[9] = "nine";
}
int main()
{
	string s;
	cin >> s;
	int ans=0;
	for (int i = 0; i < s.size(); i++)
	{
		ans += (int)s[i]-48;
	}
	string s1 = to_string(ans);
	map ItoE;//数字转英文map
	inio(ItoE);
	cout << ItoE[s1[0]-48];
	for (int i = 1; i < s1.size(); i++)
	{
		cout << ' '<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/715111.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号