1002 写出这个数 (20 分)

本文介绍了一个编程问题的解决方案,该问题要求读取一个大整数,计算其各位数字之和,并用汉语拼音的形式输出。代码使用了C++实现,通过逐个字符处理字符串形式的大整数来计算总和,并定义了一个函数将十以内的数字转换为对应的汉语拼音。

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

1002 写出这个数 (20 分)

读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

输入格式:

每个测试输入包含 1 个测试用例,即给出自然数 n 的值。这里保证 n 小于 10^​100。

输出格式:

在一行内输出 n 的各位数字之和的每一位,拼音数字间有 1 空格,但一行中最后一个拼音数字后没有空格。

输入样例:

1234567890987654321123456789

输出样例:

yi san wu

#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<cstring>
#include<cstdlib>
#include<cmath>

void f(int a)
{
	if(a==0)
		cout << "ling";
	else if(a==1)
		cout << "yi";
	else if(a==2)
		cout << "er";
	else if(a==3)
		cout << "san";
	else if(a==4)
		cout << "si";
	else if(a==5)
		cout << "wu";
	else if(a==6)
		cout << "liu";
	else if(a==7)
		cout << "qi";
	else if(a==8)
		cout << "ba";
	else if(a==9)
		cout << "jiu";
}

int main()
{
	ios_base::sync_with_stdio(0);
	string a;
	while( cin >> a )
	{
		int sum=0,len=a.length(),i;
		for(i=0;i<len;i++)
			sum+=(a[i]-'0');
		
		int w1=sum/100,w2=(sum%100-sum%10)/10,w3=sum%10;
		
		if(w1!=0)
		{
			f(w1);
			cout << ' ';
		}
		if( w1!=0 || (w2!=0&&w1==0) )
		{
			f(w2);
			cout << ' ';
		}
		f(w3);
		cout << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值