EASY_ZJU_PAT_ADVANCED LEVEL 1005

解读编程挑战:数字转英文单词之和
本文深入解析编程挑战题目,指导如何将数字转换为英文单词表示之和,涉及字符串操作、数字分解与转换等核心算法,提供详细步骤与代码实现,帮助开发者解决实际编程问题。

1005. Spell It Right (20)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
/***************************************************88
	
	@ AUTHOR: GAOMINQUAN
	@ MAIL	: ENSOLEILLY@GMAIL.COM
	@ DATA	: 2014 - 2 - 22

**************************************************/

#include<iostream>
#include<vector>
#include<string>
using namespace std;


string words[11] = {"zero","one","two","three","four","five","six","seven","eight","nine","ten"};
int digitSum(string num){
	int sum = 0;
	for(int stringIndex = 0; stringIndex <num.length(); stringIndex++){
		sum += num[stringIndex] - '0';
	}
	return sum;
}
void split(int num){
	
	vector<int> splitNum;
	if( num == 0){
		splitNum.push_back(0);
	}else{
		while(num>0){
			int tail = num%10;
			splitNum.push_back(tail);
			num /= 10;
		}
	}
	
	for(int splitIndex = splitNum.size() - 1; splitIndex > 0; splitIndex--){
		cout<<words[splitNum[splitIndex]]<<" ";
	}cout<<words[splitNum[0]];
}

int main(){
	
	string num = "1";
	cin>>num;
	int sum = digitSum(num);
	
	split(sum);

	return 0;
}
	
NOTE:

D:\MSDev98\Bin\CODE\PAT_AL_1005.CPP(34) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable co


1. 在使用string的时候,要记得把string先导入才行;
2. 数字非常长的时候,不能用int
3. 位数分解的时候,要考虑零的情况。

	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值