前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT(甲级)1005.Spell It Right(20)

PAT(甲级)1005.Spell It Right(20)

作者头像
lexingsen
发布2022-02-25 08:02:17
1920
发布2022-02-25 08:02:17
举报
文章被收录于专栏:乐行僧的博客

PAT 1002.A+B for Polynomials (25) 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.

输入格式: Each input file contains one test case. Each case occupies one line which contains an N (≤10^100​​).

输出格式: 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.

输入样例:

代码语言:javascript
复制
12345

输出样例:

代码语言:javascript
复制
one five

题目分析:首先注意题目的数据范围我用红色高亮了,N (≤10^100​​),而在C++中最大的整数是unsigned long long int是 2^64-1(18446744073709551615‬)显然是表示不了这么大(10^100)的数字的,对于大整数此时应该选择字符串或字符数组表示。 对于C/C++中基本数据的最大最小值,大家可以使用#include<climits>这个函数库。

在这里插入图片描述
在这里插入图片描述

AC代码:

代码语言:javascript
复制
#include <iostream>
#include <map>
using namesapce std;

map<int, string> mp = {{0,"zero"},{1,"one"},{2,"two"},{3,"three"},{4,"four"},{5,"five"},{6,"six"},{7,"seven"},{8,"eight"},{9,"nine"}};

int main(){
	string s;
	cin >> s;
	int sum = 0;
	for(int i=0; i<s.length(); ++i){
		sum += s[i]-'0';
	}
	s = to_string(sum);
	for(int i=0; i<s.length(); ++i){
		printf("%s",mp[s[i]-'0'].c_str());
        if(i!=s.length()-1)
            printf(" ");
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档