1005. Spell It Right (20)

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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值