PAT甲级刷题之路——A1082 Read Number in Chinese

原题如下

在这里插入图片描述
在这里插入图片描述

题目大意

给出一个不超过9个数字的整数,需要用中文读法输出,如果前面有负号,需要先输出"Fu"。注意:零需要根据中国方式来放置。

自己的想法

  1. 先通过string储存
  2. 两个非零数之间无论多少零只读一个零
  3. 尾部的零不读
  4. 第九位后加‘Yi’,第五位后加’Wan’

答案反馈

错误1

在这里插入图片描述
首先解决好解决的格式问题,发现负号后面多了一个空格,于是修正一个错误。

错误2

在这里插入图片描述
3分的测试点想必是一个特殊值,比如0,我试试哈哈
果然ac了,我真是个小机灵鬼

AC

在这里插入图片描述

代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<ctime>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<set>
#include<queue>
#include<map>
#include<stack>
using namespace std;
//const int maxn = 110;
string number[10] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
int main() {
	string str;
	cin >> str;
	if (str == "0")cout << "ling" << endl;
	else {
		int i;
		if (str[0] == '-') {
			cout << "Fu"; i = 1;
		}
		else { i = 0; }
		int len = str.length();
		int flag = 0;
		for (; i < len; i++) {
			int temp_len = len - i;
			if (len - i == 9)cout << ' ' << number[str[i] - '0'] << " Yi";
			else {
				if (flag == 1 && str[i] != '0') {
					cout << ' ' << number[0]; flag = 0;
				}
				if (str[i] == '0')flag = 1;
				if (flag == 0 && str[i] != '0') {
					if (i != 0)cout << ' ';
					cout << number[str[i] - '0'];
				}
				if (len - i == 5)cout << " Wan";
				if (str[i] != '0' && (temp_len == 8 || temp_len == 4))cout << " Qian";
				if (str[i] != '0' && (temp_len == 7 || temp_len == 3))cout << " Bai";
				if (str[i] != '0' && (temp_len == 6 || temp_len == 2))cout << " Shi";
			}
		}
		//cout << '-';
		cout << endl;
	}
	return 0;
}

结语

我发现我写代码的习惯偏向于模拟题的风格,不够灵活,考虑也不够周全,加油加油!!

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值