当前位置:网站首页>PAT乙级 1002 写出这个数
PAT乙级 1002 写出这个数
2022-08-01 04:38:00 【早睡身体好hh】
题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805324509200384
题目描述
读入一个正整数 n n n,计算其各位数字之和,用汉语拼音写出和的每一位数字。
输入格式
每个测试输入包含 1 1 1 个测试用例,即给出自然数 n n n 的值。这里保证 n n n 小于 1 0 100 10^{100} 10100。
输出格式
在一行内输出 n n n 的各位数字之和的每一位,拼音数字间有 1 1 1 空格,但一行中最后一个拼音数字后没有空格。
输入样例
1234567890987654321123456789
输出样例
yi san wu
题目解析
输入的 n
最大为 1 0 100 10^{100} 10100,超出了 int
和 long long
的表示范围,所以 n
要用字符串存储。
假设输入的 n
为 12345678 12345678 12345678,如下图所示。
那么 n
的各位数字之和 sum
即为 36 36 36,我们将 sum
的 个位 ~ 最高位
依次存储到 digits[0] ~ digits[cnt-1]
中。
最后从 sum
的最高位(即digits[cnt-1]
)开始输出,注意要单独输出 sum
的个位(末尾不加空格)。
C/C++代码
#include <iostream>
#include <string>
using namespace std;
string py[10] = {
"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
int digits[10];
int main()
{
// 输入大整数n
string n;
cin >> n;
// 计算n的各位数字之和sum
int sum = 0;
for (auto x : n)
{
sum += x - '0';
}
// 将sum的个位~最高位依次存储到digits[0]~digits[cnt-1]中
int cnt = 0;
while (sum)
{
digits[cnt++] = sum % 10;
sum /= 10;
}
// 从sum的最高位(即digits[cnt-1])开始输出
for (int i = cnt - 1; i > 0; i--)
{
cout << py[digits[i]] << " ";
}
// 单独输出sum的个位(末尾不加空格)
cout << py[digits[0]] << endl;
return 0;
}
边栏推荐
- 【愚公系列】2022年07月 Go教学课程 025-递归函数
- Software Testing Weekly (Issue 82): In fact, all those who are entangled in making choices already have the answer in their hearts, and consultation is just to get the choice that they prefer.
- The method of solving stored procedure table name passing through variable in mysql
- typescript21-接口和类型别名的对比
- 李迟2022年7月工作生活总结
- Mysql基础篇(Mysql数据类型)
- typescript23-tuple
- Unity's primary method for implementing PlanarReflection under the BuildIn rendering pipeline
- Mysql中的数据类型和运算符
- JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
猜你喜欢
基于ProXmoX VE的虚拟化家庭服务器(篇一)—ProXmoX VE 安装及基础配置
干货!如何使用仪表构造SRv6-TE性能测试环境
博客系统(完整版)
7月编程排行榜来啦!这次有何新变化?
typescript19-对象可选参数
The 16th day of the special assault version of the sword offer
typescript23-元组
This article takes you to understand the past and present of Mimir, Grafana's latest open source project
Simple and easy to use task queue - beanstalkd
typescript21-接口和类型别名的对比
随机推荐
A way to deal with infinite debugger
Passive anti-islanding-UVP/OVP and UFP/OFP passive anti-islanding model simulation based on simulink
EntityFramework saves to SQLServer decimal precision is lost
罗技鼠标体验记录
PMP工具与技术总结
safari浏览器怎么导入书签
【云原生之kubernetes实战】kubernetes集群的检测工具——popeye
typescript28 - value of enumeration type and data enumeration
MySQL4
Mysql基础篇(Mysql数据类型)
Mysql基础篇(约束)
Message queue MySQL table for storing message data
UE4 rays flashed from mouse position detection
typescript20-接口
Message Queuing Message Storage Design (Architecture Camp Module 8 Jobs)
How to promote new products online?
25. 这三道常见的面试题,你有被问过吗?
高数 | 【重积分】线面积分880例题
typescript25 - type assertion
PMP 项目质量管理