欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

1005 Spell It Right (20 分)——13行代码Ac

程序员文章站 2022-07-15 13:26:13
...

立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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 ( ≤ 1 0 1 00 ) N (≤10^100) 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;
string ss[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main() {
	string s; getline(cin, s);
	int sum = 0;
	for(auto i : s) sum += i-'0';
	
	s = to_string(sum);
	
	for(int i = 0; i < s.size(); i++) {
		if(i != 0) putchar(' ');
		cout << ss[s[i]-'0'];
	}
return 0; }
相关标签: PAT甲级 PAT甲级