1005 Spell It Right #
0、题目 #
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$ ($≤10^{100}$).
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
1、大致题意 #
给定一个非负整数 $N$ ,计算整数 $N$ 的所有位数的和 $sum$ ,然后用英文输出 $sum$ 每一位对应的英文单词。
2、基本思路 #
- 计算整数 $N$(用 $string$ 型存储,方便遍历)的每一位数字的和 $sum$( $int$型)。+ 输出 $sum$ 每一位数字对应的英文单词,把 $sum$ 转换为 $string$ 型(方便遍历),一一对应输出对应的英文单词。一一对应可以用字符串数组实现,$0-9$ 的下标对应 $0-9$ 的英文单词。
3、解题过程 #
3.1 坑点 0 #
有了思路就飞快地写代码
#include<iostream>
#include<algorithm>
using namespace std;
string n;
string a[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int b[1000];
int main(){
cin>>n;
int ans=0,c=0;
int size=n.size();
for(int i=0;i<size;i++){
ans+=n[i]-'0';
}
while(ans>0){
b[c++]=ans%10;
ans/=10;
}
if(c>=1){
cout<<a[b[c-1]];
}
for(int i=c-2;i>=0;i--){
cout<<" "<<a[b[i]];
}
}
17分,用例2错误。还是用白盒测试边界值法,想特殊值。以下就是 用例2 的坑点
输入 0
输出 zero
3.2 AC代码 #
#include<iostream>
#include<algorithm>
using namespace std;
string n;
string a[10]= {"zero","one","two","three","four","five","six","seven","eight","nine"};
int b[1000];
int main() {
cin>>n;
int ans=0,c=0;
int size=n.size();
for(int i=0; i<size; i++) {
ans+=n[i]-'0';
}
if(ans==0) {
cout<<a[0];
} else {
while(ans>0) {
b[c++]=ans%10;
ans/=10;
}
if(c>=1) {
cout<<a[b[c-1]];
}
for(int i=c-2; i>=0; i--) {
cout<<" "<<a[b[i]];
}
}
}