标签:div oid spec tor include extra lse pat input
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.
Each input file contains one test case. Each case occupies one line which contains an N (≤).
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.
12345
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
标签:div oid spec tor include extra lse pat input
原文地址:https://www.cnblogs.com/zest3k/p/11450462.html