PAT甲级-1005. Spell It Right (20)各位之和

xiaoxiao2021-02-28  37

1005. Spell It Right (20)

时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue

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 (<= 10100).

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; #define INF 0xfffffff #define MAXN 1010 string change(char x) { if(x=='0') return "zero"; else if(x=='1') return "one"; else if(x=='2') return "two"; else if(x=='3') return "three"; else if(x=='4') return "four"; else if(x=='5') return "five"; else if(x=='6') return "six"; else if(x=='7') return "seven"; else if(x=='8') return "eight"; return "nine"; } int main() { #ifdef ONLINE_JUDGE #else freopen("F:/cb/read.txt","r",stdin); //freopen("F:/cb/out.txt","w",stdout); #endif ios::sync_with_stdio(false); cin.tie(0); string s; cin>>s; long long len=s.length(); long long ans=0; for(int i=0; i<len; ++i) ans+=(s[i]-'0');//各位之和 string ss=""; if(ans==0) ss+="0"; while(ans>0) { ss+=(char(ans+'0'));//各位之和转成字符串 ans/=10; } reverse(ss.begin(),ss.end());//翻转字符串 len=ss.length(); for(int i=0; i<len-1; ++i) cout<<change(ss[i])<<" "; cout<<change(ss[len-1])<<endl; return 0; }
转载请注明原文地址: https://www.6miu.com/read-2621369.html

最新回复(0)