zoukankan      html  css  js  c++  java
  • PAT 甲级 1005 Spell It Right 模拟 字符串

    地址  https://pintia.cn/problem-sets/994805342720868352/problems/994805519074574336

    题目大意是 输入一个很大的非负整数  0 <= N <=  10100

    要求我们把该数的每位上的数字相加,并且按照英文输出每位上的数字

    输入样例:
    12345
    输出样例:
    one five

    示例解释  因为1+2+3+4+5 =15  所以输出 one five

    题目主要考核 数字和字符串的转换
    我们输入的字符串(不是数字因为数据范围太大了)逐个转换成数字相加
    然后将相加的和拆解成单个的数字 找出对应的字符串 输出即可

            {1,"one"},
            {2,"two"},
            {3,"three"},
            {4,"four"},
            {5,"five"},
            {6,"six"},
            {7,"seven"},
            {8,"eight"},
            {9,"nine"},
            {0,"zero"}    

    代码

    #include <iostream>
    #include <string>
    #include <vector>
    #include <map>
    #include <algorithm>
    using namespace std;
    
    int main(){
        string s;
        cin >> s;
        int num = 0;
    for(auto& e:s){
        num += e-'0';
    }
    map<int,string> m{
    {1,"one"},
    {2,"two"},
    {3,"three"},
    {4,"four"},
    {5,"five"},
    {6,"six"},
    {7,"seven"},
    {8,"eight"},
    {9,"nine"},
    {0,"zero"}
    };
    vector<string> ans;
    if(num==0){
        cout << "zero" <<endl;
        return 0;
    }
    while(num!=0){
        int idx = num%10;
        num = num/10;
        ans.push_back(m[idx]);
    }
    reverse(ans.begin(),ans.end());
    for(int i = 0; i<ans.size();i++){
        cout << ans[i];
        if(i!=ans.size()-1){
            cout << " ";
        }
    }
    cout<<endl;
    return 0;
    }    
    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    蓝书3.6 割点与桥
    蓝书3.5 强连通分量
    蓝书3.4 差分约束系统
    蓝书3.3 SPFA算法的优化
    蓝书3.2 最短路
    蓝书3.1 最小生成树
    luogu 4630 [APIO2018] Duathlon 铁人两项
    Codeforces Round #124 (Div. 1) C. Paint Tree(极角排序)
    dutacm.club Water Problem(矩阵快速幂)
    dutacm.club 1094: 等差区间(RMQ区间最大、最小值,区间GCD)
  • 原文地址:https://www.cnblogs.com/itdef/p/14396075.html
Copyright © 2011-2022 走看看