PAT甲级1005:Spell It Right (20)
题目
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
解题思路
首先先求和,再将和拆分为每一位的数字,最后输出每一位数字对应的英文即可。
易错点
测试点2:当和为0时,需要输出“zero”。
代码
#include<bits/stdc++.h> using namespace std; int main(){ long int sum=0; int i,t,top; char temp; vector <int> p; char number[10][6] = { "zero","one","two","three","four","five","six","seven","eight","nine"}; while (scanf("%c",&temp)!=EOF){ //读取 if (temp== ) break; else sum+=(temp-48); } if (sum==0)//测试点2 printf("zero"); else { while (sum!=0){ //拆分为每一位的数字 p.push_back(sum%10); sum/=10; } top = p.size(); for (i=top-1;i>=0;i--) //输出 { t = p[i]; if (i!=top-1) printf(" "); printf("%s",number[t]); } } return 0; }
上一篇:
IDEA上Java项目控制台中文乱码