当前位置: 代码迷 >> 综合 >> 1005 Spell It Right(20分)
  详细解决方案

1005 Spell It Right(20分)

热度:38   发布时间:2023-11-28 05:44:43.0

题目链接1005 Spell It Right

题意

给出字符串,计算每位上的数字之和,然后用英语输出和。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
const int maxn = 110;
void toDigit(int sum);
void change(int mod, char arr[]);
int main(int argc, char** argv) {
    char str[maxn];	scanf("%s", str);int sum = 0;for (int i = 0; i < strlen(str); i++) {
    sum += str[i] - '0';}toDigit(sum);return 0;
}
void toDigit(int sum) {
    char arr[maxn];int mod = sum % 10;if (sum / 10 ==  0) {
    change(mod, arr);printf("%s", arr);return ;}toDigit(sum / 10);change(mod, arr);printf(" %s", arr);
}
void change(int mod, char arr[]) {
    string str;switch (mod) {
    case 0: str = "zero"; break;case 1: str = "one"; break;case 2: str = "two"; break;case 3: str = "three"; break;case 4: str = "four"; break;case 5: str = "five"; break;case 6: str = "six"; break;case 7: str = "seven"; break;case 8: str = "eight"; break;case 9: str = "nine"; break;}int i;for (i = 0; i < str.size(); i++)arr[i] = str.at(i);arr[i] = '\0';
}
  相关解决方案