当前位置:网站首页>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;
}
边栏推荐
- Immutable
- 开源许可证 GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
- Excel record of integer programming optimization model to solve the problem
- Lawyer Interpretation | Guns or Roses?Talking about Metaverse Interoperability from the Battle of Big Manufacturers
- 万字逐行解析与实现Transformer,并进行德译英实战(三)
- 请问表格储存中用sql只能查询到主键列,ots sql非主键不支持吗?
- leetcode:126. Word Solitaire II
- MySQL-数据操作-分组查询-连接查询-子查询-分页查询-联合查询
- (more than 2022 cattle school four) A - Task Computing + dynamic programming (sort)
- typescript27-枚举类型呢
猜你喜欢

出现Command ‘vim‘ is available in the following places,vim: command not found等解决方法

(2022牛客多校四)D-Jobs (Easy Version)(三维前缀或)

typescript28 - value of enumeration type and data enumeration

剑指 Offer 68 - I. 二叉搜索树的最近公共祖先

scheduleWithFixedDelay和scheduleAtFixedRate的区别

数组问题之《两数之和》以及《三数之和 》

What is dynamic programming and what is the knapsack problem

高数 | 【重积分】线面积分880例题

Logitech Mouse Experience Record

ICML2022 | Deep Dive into Permutation-Sensitive Graph Neural Networks
随机推荐
请问表格储存中用sql只能查询到主键列,ots sql非主键不支持吗?
25. 这三道常见的面试题,你有被问过吗?
【愚公系列】2022年07月 .NET架构班 085-微服务专题 Abp vNext微服务网关
挑战52天背完小猪佩奇(第01天)
Message queue design based on mysql
文件的异步读写
基于Arduino制作非接触式测温仪
数组问题之《下一个排列》、《旋转图像》以及二分查找之《搜索二维矩阵》
Risk strategy important steps of tuning method
Valentine's Day Romantic 3D Photo Wall [with source code]
MySQL Practice Summary -
【kali-信息收集】枚举——DNS枚举:DNSenum、fierce
【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
[FPGA tutorial case 43] Image case 3 - image sobel edge extraction through verilog, auxiliary verification through MATLAB
万字逐行解析与实现Transformer,并进行德译英实战(三)
56:第五章:开发admin管理服务:9:开发【文件上传到,MongoDB的GridFS中,接口】;(把文件上传到GridFS的SOP)
Visual Studio提供的 Command Prompt 到底有啥用
报错:AttributeError: module ‘matplotlib’ has no attribute ‘figure’
Excel record of integer programming optimization model to solve the problem
The difference between scheduleWithFixedDelay and scheduleAtFixedRate