当前位置: 代码迷 >> 综合 >> PAT甲级 - 1005 Spell It Right (20 分)
  详细解决方案

PAT甲级 - 1005 Spell It Right (20 分)

热度:66   发布时间:2023-11-02 19:48:33.0

题目链接:PAT甲级 - 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 (≤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 <iostream>
#include <cstdio>
#include <cmath>
#include <stack>
using namespace std;
const int maxn=510;
const int INF=0x3f3f3f3f;
string s; int main(){string ans[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};while(cin>>s){int len=s.length();int sum=0;for(int i=0;i<len;i++){sum+=s[i]-'0';}stack<int> s;if(sum==0) s.push(0);  //漏掉这个情况,测试点2通不过,3分 while(sum){int x=sum%10;s.push(x);sum/=10;}bool flag=false;while(!s.empty()){int x=s.top();s.pop();if(!flag){cout<<ans[x];flag=true;}else{cout<<" "<<ans[x];}}cout<<endl;}return 0;
}

 

  相关解决方案