1005. Spell It Right

本文介绍了一个将输入字符串中的所有数字字符相加,并将得到的数字用英文单词表示出来的C++程序。该程序利用了标准库中的各种功能,如字符串操作和映射表来实现从数字到英文单词的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 1005. Spell It Right.cpp: 主项目文件。

#include "stdafx.h"
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iostream>
using namespace std;

map<int,string> cmap;

void operate(int sum){
	if(sum==0){
		puts("zero");
		return;
	}
	int aa[4],cnt=0;
	memset(aa,0,sizeof(aa));
	while(sum){
		aa[cnt++]=sum%10;
		sum/=10;
	}
	for(int i=cnt-1;i>=0;i--){
		if(i!=cnt-1)
			cout<<" ";
		cout<<cmap[aa[i]];
	}
	cout<<endl;
}

int main()
{
    const int N=103;
	char str[N];
	cmap[0]="zero";cmap[1]="one";cmap[2]="two";cmap[3]="three";
	cmap[4]="four";cmap[5]="five";cmap[6]="six";cmap[7]="seven";
	cmap[8]="eight";cmap[9]="nine";
	while(gets(str)){
		int sum=0;
		for(int i=0;str[i];i++)
			sum+=str[i]-'0';
		operate(sum);
	}
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值