当前位置:网站首页>PAT serie b write the number 1002
PAT serie b write the number 1002
2022-08-01 04:50:00 【Go to bed early and feel good 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
With a string stored.
假设输入的 n
为 12345678 12345678 12345678,如下图所示.
那么 n
的各位数字之和 sum
即为 36 36 36,我们将 sum
的 个位 ~ 最高位
依次存储到 digits[0] ~ digits[cnt-1]
中.
最后从 sum
的最高位(即digits[cnt-1]
)开始输出,Be careful to separate output sum
的个位(The end does not add Spaces).
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的个位~Highest stored in order todigits[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的个位(The end does not add Spaces)
cout << py[digits[0]] << endl;
return 0;
}
边栏推荐
- 博客系统(完整版)
- 剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
- In the shake database, I want to synchronize the data of the source db0 to the destination db5, how to set the parameters?
- typescript25-类型断言
- Valentine's Day Romantic 3D Photo Wall [with source code]
- PMP 项目沟通管理
- Message Queuing Message Storage Design (Architecture Camp Module 8 Jobs)
- PMP 项目质量管理
- 高数 | 【重积分】线面积分880例题
- 25. Have you been asked these three common interview questions?
猜你喜欢
基于STM32设计的UNO卡牌游戏(双人、多人对战)
基于ProXmoX VE的虚拟化家庭服务器(篇一)—ProXmoX VE 安装及基础配置
Message queue MySQL table for storing message data
Excel做题记录——整数规划优化模型
y83. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (14)
【无标题】
Mysql基础篇(Mysql数据类型)
Step by step hand tearing carousel Figure 3 (nanny level tutorial)
【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
高数 | 【重积分】线面积分880例题
随机推荐
怀念故乡的面条
typescript23-tuple
Difference Between Compiled and Interpreted Languages
typescript24-类型推论
lambda
李迟2022年7月工作生活总结
"ArchSummit: The cry of the times, technical people can hear"
Game Theory (Depu) and Sun Tzu's Art of War (42/100)
Pyspark机器学习:向量及其常用操作
基于STM32设计的UNO卡牌游戏(双人、多人对战)
Visual Studio提供的 Command Prompt 到底有啥用
y83.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十四)
typescript28-枚举类型的值以及数据枚举
The difference between scheduleWithFixedDelay and scheduleAtFixedRate
PMP 项目资源管理
一个往年的朋友
PMP工具与技术总结
7 行代码搞崩溃 B 站,原因令人唏嘘!
【堆】小红的数组
【愚公系列】2022年07月 Go教学课程 024-函数