原题如下
题目大意
给出一个不超过9个数字的整数,需要用中文读法输出,如果前面有负号,需要先输出"Fu"。注意:零需要根据中国方式来放置。
自己的想法
- 先通过string储存
- 两个非零数之间无论多少零只读一个零
- 尾部的零不读
- 第九位后加‘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;
}
结语
我发现我写代码的习惯偏向于模拟题的风格,不够灵活,考虑也不够周全,加油加油!!