您的位置:首页 > 其它

1005. Spell It Right (20)

2017-01-04 16:57 211 查看
pat

题目大概意思是,接收输入字符串,然后把每一项加起来,然后再按规定格式把和输出。

解题心得:

1、一道简单题,但貌似c++中字符串相关忘得差不多,也复习一把。

2、没注意全为0的特殊情况,导致第一次提交没通过

代码如下:

#include"stdio.h"
#include"string.h"
#include"math.h"
#include<iostream>
#include<vector>
#include<queue>
#include<string>
using namespace std;
#define INF 0x7FFFFFFF

char format[10][20] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
void splitAndPrint(int num){
vector<int> out;
out.resize(10);
out.assign(10,-1);
int i = 0;
while(true){
int digit = num % 10;
num = num /10;
out[i] = digit;
i++;
if(num == 0){
break;
}
}
int length = out.size();
for(int j = length-1;j >= 0;j--){
if(out[j] == -1){
continue;
}
if(j == 0){
printf("%s",format[out[j]]);
}else{
printf("%s ",format[out[j]]);
}
}
}

int main(){
char input[110];
while(scanf("%s",input) != EOF){
int length = strlen(input);
int sum = 0;
for(int i = 0;i < length;i++){
int temp = input[i] - '0';
sum += temp;
}
splitAndPrint(sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: