1005 Spell It Right

1005 Spell It Right

0、题目

Given a non-negative integer N N N, your task is to compute the sum of all the digits of N N 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 N N ( ≤ 1 0 100 ≤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

1、大致题意

给定一个非负整数 N N N ,计算整数 N N N 的所有位数的和 s u m sum sum ,然后用英文输出 s u m sum sum 每一位对应的英文单词。

2、基本思路

  1. 计算整数 N N N(用 s t r i n g string string 型存储,方便遍历)的每一位数字的和 s u m sum sum i n t int int型)。
  2. 输出 s u m sum sum 每一位数字对应的英文单词,把 s u m sum sum 转换为 s t r i n g string string 型(方便遍历),一一对应输出对应的英文单词。一一对应可以用字符串数组实现, 0 − 9 0-9 09 的下标对应 0 − 9 0-9 09 的英文单词。

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]];
		}
	}
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值