PAT (Advanced Level) Practice 1005 Spell It Right

PAT (Advanced Level) Practice 1005 Spell It Right
题目大意 读入一串数字,然后逐位求和,然后用英文输出这个和的每一位
思路

  1. string读入字符串
  2. 用num处理字符串的和
  3. 逐位求num的值存放在ary数组中
  4. 我们输出ary数组中对应的英文单词即可
    注意0的情况
#include<iostream> 
#include<string>
#include<stdio.h>
using namespace std;
string sa[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
const int maxn=1000;
int main()
{
	string str;
	cin>>str;
	int ary[maxn];
	int num=0;
	int index=0;
	for(int i=str.size()-1;i>=0;i--)
	{
		 num+=str[i]-'0';
	}
	if(num==0)
	{
		cout<<"zero";
		return 0;
	}
	while(num)
	{
		ary[index++]=num%10;
		num/=10;
	}
	for(int i=index-1;i>=0;i--)
	{
		if(i!=index-1)cout<<" ";
		cout<<sa[ary[i]];
	}
	return 0;
	
}

上一篇:Diaries_OnTest


下一篇:A+B for Input-Output Practice (IV)