• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
村雨sup
自己选的路,跪着也要走完 XD
博客园    首页    新随笔    联系   管理    订阅  订阅
PAT 1005 Spell It Right
1005 Spell It Right (20 分)
 

Given a non-negative integer N, your task is to compute the sum of all the digits of 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 (≤).

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

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<string> vec={{"zero"},{"one"},{"two"},{"three"},{"four"},{"five"},{"six"},{"seven"},{"eight"},{"nine"}};

vector<int> temp;


int main(){
    string s;
    cin >> s;
    ll sum = 0;
    for(int i=0;i < s.size();i++){
        sum += (s[i]-'0');
    }
    if(sum == 0) cout << vec[0];
    while(sum){
        temp.push_back(sum%10);
        sum = sum/10;
    }
    for(int i=temp.size()-1;i>=0;i--){
        cout << vec[temp[i]];
        if(i) cout << " ";
    }


    return 0;
}

sum = 0的情况要注意,不会进入循环。

posted on 2019-04-09 00:56  村雨sup  阅读(86)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3