PAT Advanced 1005 Spell It Right
Posted zest3k
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT Advanced 1005 Spell It Right相关的知识,希望对你有一定的参考价值。
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
求N的每一位的和,并以英文输出和的每一位数字。由于N最多有101位,这里的add函数可以直接累加,不需要高精度加法。
#include <bits/stdc++.h> using namespace std; void add(vector<int> &digits,int b) int res=b; int ptr=0,len=digits.size(); while(res!=0) if(ptr<len) digits[ptr]+=res; res=digits[ptr]/10; digits[ptr]%=10; ptr++; else digits.push_back(res); res=0; int main() map<int,string> ans; ans[0]="zero"; ans[1]="one"; ans[2]="two"; ans[3]="three"; ans[4]="four"; ans[5]="five"; ans[6]="six"; ans[7]="seven"; ans[8]="eight"; ans[9]="nine"; string N; cin>>N; int len=N.length(); vector<int> digits; digits.push_back(0); for(int i=len-1;i>=0;i--) add(digits,N[i]-‘0‘); len=digits.size(); cout<<ans[digits[len-1]]; for(int i=len-2;i>=0;i--) cout<<‘ ‘<<ans[digits[i]]; cout<<endl; return 0;
以上是关于PAT Advanced 1005 Spell It Right的主要内容,如果未能解决你的问题,请参考以下文章