码迷,mamicode.com
首页 > 其他好文 > 详细

PAT 甲级 1005 Spell It Right

时间:2018-08-05 00:33:37      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:mes   namespace   scan   you   git   case   pat   tar   must   

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

 

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 (<= 10^100^).

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;

char s[1111], num[1111];
char a[20][50] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

void itoa(int x) {
    if(x == 0) {
        num[0] = ‘0‘;
        num[1] = 0;
        return ;
    }
    stack<int> st;
    while(x) {
        st.push(x % 10);
        x = x / 10;
    }
    int sz = 0;
    while(!st.empty()) {
        num[sz++] = (char)(st.top() + ‘0‘);
        num[sz] = 0;
        st.pop();
    }
}

int main() {
    scanf("%s", s);
    int len = strlen(s);
    int sum = 0;
    for(int i = 0; i < len; i ++) {
        sum += s[i] - ‘0‘;
    }

    itoa(sum);
    int lenum = strlen(num);

    for(int i = 0; i < lenum; i ++) {
        printf("%s", a[num[i] - ‘0‘]);
        printf("%s", i != lenum - 1 ? " " : "\n");
    }
    return 0;
}

  

PAT 甲级 1005 Spell It Right

标签:mes   namespace   scan   you   git   case   pat   tar   must   

原文地址:https://www.cnblogs.com/zlrrrr/p/9420593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!