当前位置:网站首页>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;
}
边栏推荐
- typescript23-元组
- 风险策略调优中重要的三步分析法
- The Principle Of Percona Toolkit Nibble Algorithm
- ModuleNotFoundError: No module named ‘tensorflow.keras‘报错信息的解决方法
- 挑战52天背完小猪佩奇(第01天)
- UE4 模型OnClick事件不生效的两种原因
- 故乡的素描画
- Excel做题记录——整数规划优化模型
- EntityFramework saves to SQLServer decimal precision is lost
- "ArchSummit: The cry of the times, technical people can hear"
猜你喜欢

UE4 制作遇到的问题

最新 955 不加班的公司名单

Pyspark机器学习:向量及其常用操作

Excel record of integer programming optimization model to solve the problem

typescript21 - Comparison of Interfaces and Type Aliases

博客系统(完整版)

万字逐行解析与实现Transformer,并进行德译英实战(一)

Simulation of Active anti-islanding-AFD Active Anti-islanding Model Based on Simulink

【愚公系列】2022年07月 Go教学课程 025-递归函数

Risk strategy important steps of tuning method
随机推荐
数组问题之《两数之和》以及《三数之和 》
Write a method to flatten an array and deduplicate and sort it incrementally
【愚公系列】2022年07月 Go教学课程 025-递归函数
Swastika line-by-line parsing and realization of the Transformer, and German translation practice (2)
PMP 80个输入输出总结
(2022牛客多校四)N-Particle Arts(思维)
Pyspark机器学习:向量及其常用操作
56:第五章:开发admin管理服务:9:开发【文件上传到,MongoDB的GridFS中,接口】;(把文件上传到GridFS的SOP)
智芯传感输液泵压力传感器 为精准智能控制注入科技“强心剂”
typescript23-元组
MySQL-DML语言-数据库操作语言-insert-update-delete-truncate
数据比对功能调研总结
Visual Studio提供的 Command Prompt 到底有啥用
typescript28-枚举类型的值以及数据枚举
Message Queuing Message Storage Design (Architecture Camp Module 8 Jobs)
Dynamic Programming 01 Backpack
The method of solving stored procedure table name passing through variable in mysql
Flink 1.13 (8) CDC
深圳某游戏研发公司给每个工位都装监控,网友:堪比坐牢!
API Design Notes: The pimpl trick