PAT乙级——1002 写出这个数

上题目:
在这里插入图片描述
输入的n很大,这里我考虑用字符串读取,然后遍历字符串中每一个字符,在遍历的过程正好可以把数字之和求出来。求出来之后应当是一个整型变量。

这时我想到的是把这个整型变量传入一个字符流里面,然后再定义一个字符串来取字符流里面的字符,最后遍历字符串,输入拼音

思路暂时是这样,肯定还会又更简洁的思路的!

话不多说,上代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>

using namespace std;

int main()
{
	//输入
	string str1;
	cin >> str1;

	int sum = 0;

	//计算总和
	for (int i = 0; i < str1.length(); ++i) {
		sum += str1[i] - '0';
	}

	//将sum输入到字符流中
	stringstream ss;
	ss << sum;

	//取字符流中的字符
	string str2 = ss.str();

	//输出拼音,会很长(我用if-else if)
	//由于我每个拼音后面都跟了空格,因此可能会出错
	//果然出错了,那怎么把最后的空格去掉呢?
	//最后再添加空格就好了,如果是i==str2.length()-1,那不输出空格
	for (int i = 0; i < str2.length(); ++i) {
		if (str2[i] == '0') {
			cout << "ling";
		}
		else if (str2[i] == '1') {
			cout << "yi";
		}
		else if (str2[i] == '2') {
			cout << "er";
		}
		else if (str2[i] == '3') {
			cout << "san";
		}
		else if (str2[i] == '4') {
			cout << "si";
		}
		else if (str2[i] == '5') {
			cout << "wu";
		}
		else if (str2[i] == '6') {
			cout << "liu";
		}
		else if (str2[i] == '7') {
			cout << "qi";
		}
		else if (str2[i] == '8') {
			cout << "ba";
		}
		else if (str2[i] == '9') {
			cout << "jiu";
		}
		
		//输出空格
		if (i != str2.length() - 1) {
			cout << " ";
		}
	}
	
	return 0;
}

不说了,不说了,干饭干饭!!!🍞

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值