【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;
}
相关推荐
_Twink1e10 小时前
openJiuwen 实训营 Day 1 · WorkSwarm 入门教程
开发语言·c++·openjiuwen
dong_junshuai11 小时前
每天一个开源项目#87 fmt:24.5K Stars 的 C++ 格式化核心
c++·开源·github
雪的季节11 小时前
Multisim14.0的详细中文安装步骤【附安装包】
c++
Persistent的粽子!12 小时前
C++:类与对象(二)
开发语言·c++
蒸蒸yyyyzwd13 小时前
cpp 选手备战秋招学习笔记 day23
c++·求职招聘
.YM.Z13 小时前
C++ STL 容器深度剖析:vector 与 list 的模拟实现及对比
开发语言·c++·vector·list
mengzhi啊15 小时前
QEventLoop loop配合loop.exec();替代while(1)。电脑cpu占用为0,使用while(1)cpu占用率100%
c++·qt
HugoStudio_SWAN15 小时前
洛谷 P1319 / P1320 压缩技术——同一枚硬币的正反面
c++·学习·程序人生·算法
小小晓.15 小时前
C++单例模式万字详解:6种实现演进+线程安全彻底解决+CRTP终极模板
c++·单例模式
j7~16 小时前
【C++11】C++11 新特性全套详解(列表初始化、右值引用、lambda 表达式、function 与 bind 包装器等)
c++·右值引用·lambda表达式·可变参数模板·移动语义·包装器·c++11发展史