您的位置:首页 > 其它

1005. Spell It Right (20)

2017-02-15 12:49 288 查看
分析:字符串转数字。每一位数字再映射成英文。用sprintf()一下子就解决了。

代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;

char num[15][10] = {
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
};

int main() {
char ch = ' ';
int sum = 0;
while(true) {
scanf("%c", &ch);
if(ch == '\n') break;
sum = sum + ch - '0';
}

char str[200];
sprintf(str, "%d", sum);
int len = strlen(str);
for(int i = 0; i < len-1; ++i)
printf("%s ", num[str[i]-'0']);
printf("%s\n", num[str[len-1]-'0']);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT