【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;
}
相关推荐
郝学胜_神的一滴3 小时前
CMake 034:生成器表达式:解耦构建时序、精简分支逻辑的终极利器
c++·cmake
见过夏天18 小时前
C++ 基础入门完全指南
c++
用户805533698032 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
BadBadBad__AK3 天前
线段树维护区间 k 次方和
c++·数学·算法·stl
卷无止境3 天前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
卷无止境3 天前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
郝学胜_神的一滴4 天前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境6 天前
C++ 的Eigen 库全解析
c++
卷无止境6 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端
郝学胜_神的一滴6 天前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake