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

1005 Spell It Right (20 分)

Given a non-negative integer NNN, your task is to compute the sum of all the digits of NNN, 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 NNN (≤10100\le 10^{100}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

给一端长度最多为101的数字串,每个位置的数字相加后将其结果用英文表示(把结果的每位数字用英文替换)

思路:模拟

Code

#include <bits/stdc++.h>

using namespace std;

stack<string> stk;
string change[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

int main() {
  string num;
  long long sum = 0;
  cin >> num;
  for (int i = num.size() - 1; i >= 0; i--) {
    sum += num[i] - '0';
  }
  while (!stk.empty()) {
    stk.pop();
  }
  do {
    stk.push(change[sum % 10]);
    sum /= 10;
  }while(sum);
  cout << stk.top();
  stk.pop();
  while (!stk.empty()) {
    cout << " " << stk.top();
    stk.pop();
  }
  cout << endl;
  return 0;
}
全部评论

相关推荐

程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务