【PAT甲级真题】- Spell It Right(20)

题目来源

Spell It Right

题目描述点击链接自行查看

注意点:

  • 末尾无空格,多换一行

Description

Given a non-negative integer N N N, your task is to compute the sum of all the digits of N N N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N ( ≤ 10 100 ) N (≤10 ^{100}) N(≤10100).

Output Specification:

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.

Sample Input:

复制代码
12345

Sample Output:

复制代码
one five

题目大意

给出一个很大的整数 N N N ,计算它每个位的数字之和,然后从左到右,输出这个和的每个位的数字对应的英文

思路简介

一道打表题

对于数字转字母的处理就用一个字符串数组打表

cpp 复制代码
string dig[]={
    "zero","one","two","three",
    "four","five","six","seven",
    "eight","nine"
};

输入记得输字符串,整数无法直接输入,10\^{100} 超出范围了

求和时,数字字符减去 '0' 即为其数字的数值

求和后对照表输出即可

遇到的问题

  1. 无,一遍过

代码

cpp 复制代码
/**
 * https://pintia.cn/problem-sets/994805342720868352/exam/problems/type/7?problemSetProblemId=994805519074574336
 * 打表
 */
#include<bits/stdc++.h>
using namespace std;

string dig[]={
    "zero","one","two","three",
    "four","five","six","seven",
    "eight","nine"
};

void solve(){
    string n;cin>>n;   
    int sum=0;
    for(auto x:n){
        sum+=x-'0';
    }
    string res=to_string(sum);
    int len=res.size();
    for(int i=0;i<len;++i){
        cout<<dig[res[i]-'0'];
        if(i!=len-1)cout<<' ';
    }
    cout<<'\n';
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //fstream in("in.txt",ios::in);cin.rdbuf(in.rdbuf());
    int T=1;
    //cin>>T;
    while(T--){
        solve();
    }
    return 0;
}
相关推荐
TANGLONG2221 小时前
【C++】STL基础必备:深入解析vector容器的实现(含源码)
c语言·开发语言·数据结构·c++·笔记·算法·stl
慢慢向上的蜗牛1 小时前
Atlas300I推理卡驱动适配Linux 6.12+内核
linux·c++·人工智能·华为·驱动·底层开发·ascend
码农小韩1 小时前
QT学习记录(三)——C++学习基础(三)
开发语言·c++·qt·学习·算法·嵌入式软件
承渊政道1 小时前
【动态规划算法】(似包非包以及卡特兰数问题深入解析)
数据结构·c++·学习·算法·leetcode·动态规划·哈希算法
楼田莉子1 小时前
仿Muduo的高并发服务器:基于Tcp协议的回显服务器
linux·服务器·c++·后端
CSCN新手听安2 小时前
【Qt】系统相关(二)鼠标事件的处理,鼠标的按下,释放,双击,移动,滚轮滚动事件的处理
开发语言·c++·qt
承渊政道2 小时前
【动态规划算法】(一文讲透二维费用的背包问题)
数据结构·c++·学习·算法·leetcode·动态规划·哈希算法
2301_815279522 小时前
鸿蒙原生开发的“硬核通道”:ArkTS 与 C/C++ 高性能互操作全栈指南 —— FFI 机制深度解析与实战精要
c语言·c++·harmonyos
Je1lyfish11 小时前
CMU15-445 (2025 Fall/2026 Spring) Project#3 - QueryExecution
linux·c语言·开发语言·数据结构·数据库·c++·算法