1005. Spell It Right

1005. Spell It Right (20)

时间限制
400 ms
内存限制
65536 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
题目是简单的字符串操作,题目意思是给一个数字字符串,计算各位之和sum,并将sum各位从高位到低位每位用英文表示,在计算sum都可以做到,然后就在于用英文如何去表示了,我用了两种方法,一种讲int类型转换为string,另外一种,用vector。代码分别如下:

代码一:int类型转换为string类型

#include<iostream>
#include<string>
#include<sstream>
#include<stdio.h>
using namespace std;

int main()
{
	freopen("E://input.txt", "r", stdin);
	string s;
	int sum = 0;
	cin>>s;
	
	string str[10] = 
	{
		"zero",
		"one",
		"two",
		"three",
		"four",
		"five",
		"six",
		"seven",
		"eight",
		"nine",
	};
	
	for(int i = 0; i < s.length(); i ++)
		sum = sum + s[i] - '0';
	
	
	ostringstream oss;
	oss<<sum;
	s = oss.str();
	
	for(int i = 0; i < s.length(); i ++)
	{
		if(i == 0)
			cout<<str[s[i] - '0'];
		else
			cout<<" "<<str[s[i] - '0'];
	}
	cout<<endl;
	return 0;
}

代码二:vector
#include<iostream>
#include<string>
#include<sstream>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
	freopen("E://input.txt", "r", stdin);
	string s;
	int sum = 0;
	cin>>s;
	
	string str[10] = 
	{
		"zero",
		"one",
		"two",
		"three",
		"four",
		"five",
		"six",
		"seven",
		"eight",
		"nine",
	};
	
	for(int i = 0; i < s.length(); i ++)
		sum = sum + s[i] - '0';
	
	if(sum == 0)
		cout<<"zero";
	
	vector<string> vs;
	
	while(sum)
	{
		vs.push_back(str[sum%10]);
		sum /= 10;
	}
	
	reverse(vs.begin(), vs.end());
	vector<string>::iterator it;
	for(it = vs.begin(); it != vs.end(); it ++)
	{
		if(it == vs.begin())
			cout<<*it;
		else
			cout<<" "<<*it;
	}
	cout<<endl;
	return 0;
}



你可能感兴趣的:(1005. Spell It Right)