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;
}
题目大概意思是,接收输入字符串,然后把每一项加起来,然后再按规定格式把和输出。
解题心得:
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;
}
相关文章推荐
- PAT-A-1005. Spell It Right (20)
- 1005. Spell It Right (20)
- PAT 1005. Spell It Right(20)
- PAT(甲级)1005. Spell It Right (20)
- PAT (Advanced Level) 1005. Spell It Right (20) 解题报告
- PAT 1005. Spell It Right (20)
- (PAT)1005. Spell It Right (20)
- 1005. Spell It Right (20)——PAT (Advanced Level) Practise
- 1005. Spell It Right (20)
- 1005. Spell It Right (20)(C++)
- PAT (Advanced Level) Practise - 1005. Spell It Right (20)
- 1005. Spell It Right (20)
- 1005. Spell It Right (20)——PAT (Advanced Level) Practise
- PAT 1005. Spell It Right (20)
- 1005. Spell It Right (20)
- 1005. Spell It Right (20)
- PAT甲级-1005. Spell It Right (20)各位之和
- 1005. Spell It Right (20)
- Python 实现Pat 1005. Spell It Right (20)
- 1005. Spell It Right (20)