1005 Spell It Right (20 分)

这道题是考查字符串的操作,需要注意的是PTA系统只能在c编译环境下使用gets接收一行字符串,在C++使用gets会出错。本题既可以用纯C做,也可以结合map来写,总体来说还是比较简单的。

版本2

#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <algorithm> 
#include <unordered_set>
#include <unordered_map>
#include <map>
using namespace std;
int main(){
	string str;
	cin>>str;
	unordered_map<char, string> mp;
	mp['1'] = "one";
	mp['2'] = "two";
	mp['3'] = "three";
	mp['4'] = "four";
	mp['5'] = "five";
	mp['6'] = "six";
	mp['7'] = "seven";
	mp['8'] = "eight";
	mp['9'] = "nine";
	mp['0'] = "zero";
	int sum = 0;
	for(auto c: str){
		sum += (c -'0');
	}
	str = to_string(sum);
	for(int i = 0; i < str.size(); i++){
		char c =  str[i];
		if(i==0) cout<<mp[c];
		else cout<<" "<<mp[c]; 
	}
	return 0;
}

版本1

#include<cstdio>
#include<cstdlib>  //有数字转字符串的函数,可以直接调用
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int arr[100];
int k=0;
void change(int x){
	do{
		arr[k++]=x%10;
		x=x/10;
	}while(x!=0);
}
int main(){
	string str;
	map< int, string > mp;
//	mp.insert(make_pair(0,"zero")); 另一种方法
	mp[0]="zero";
	mp[1]="one";
	mp[2]="two";
	mp[3]="three";
	mp[4]="four";
	mp[5]="five";
	mp[6]="six";
	mp[7]="seven";
	mp[8]="eight";
	mp[9]="nine";
	cin>>str;
	int sum=0;
	for(int i=0;i<str.size();i++){
		sum+=str[i]-'0';
	}
	change(sum);
	for(int j=k-1;j>=0;j--){
		cout<<mp[arr[j]];
		if(j>0) printf(" ");
	}
	printf("\n");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值