b( ̄▽ ̄)d ,水题一道,没有难度。
看原题:
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
意思就是给你一个正整数,你把它的每位数字求和,按照顺序把和用英文输出。可以用string输入字符串,写个函数输出。没什么可说的。
#include <iostream>
#include <string>
using namespace std;
void print(
int num,
const string *Number)
{
int temp = num;
int count =
0;
int t =
1;
while (temp >
9)
{
temp /=
10;
++count;
t *=
10;
}
temp = num;
cout << Number[temp / t];
temp -= temp / t * t;
t /=
10;
--count;
while (count >=
0)
{
cout <<
" " << Number[temp / t];
temp -= temp / t * t;
t /=
10;
--count;
}
cout << endl;
}
int main(
void)
{
const string Number[
10] = {
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine" };
string str;
int sum =
0;
cin >> str;
for (
int i =
0; i < (
int)str.length(); ++i)
{
sum += str[i] -
'0';
}
print(sum, Number);
return 0;
}
好好学习,天天向上
转载请注明原文地址: https://ju.6miu.com/read-15674.html