题目链接https://pintia.cn/problem-sets/994805342720868352/problems/994805385053978624
非常恶心的字符串处理,也只有PAT里能见到了。。
建议四位四位来处理,即
9 | 8 7 6 5 | 4 3 2 1
干脆写成9个位各自处理了。。
预处理
首先确定符号,如果负号存在就将其从字符串内除去
bool isFu = false;if (str[0] == '-') {
str.erase(0, 1);isFu = true;}
将字符串用0填满9位,用arr[]
数组存每个位上的数字,用flag[]
存该位是否为0,true
表示非零,flase
表示为零。
// fill 9 digitsif (str.size() < 9)str.insert(0, 9 - str.size(), '0');for (int i = 0; i < 9; i++) {
arr[i] = str[i] - '0';if (arr[i] != 0)flag[i] = true;}
——接下来开始输出——
符号位
last_part
表示是否为【非首次输出】,因为首次输出不要在前面加空格,非首次输出要在前面加空格。因为数字可能有1~9位,首次输出可能出现在任何一位,因此都需要对last_part
进行判断一次。输出后,以后都是非首次输出了,把它置为true
// handle Fulong int num = stoi(str);if (num != 0 && isFu) {
printf("Fu");last_part = true;}
第9位
只需管亿位是否为0,即flag[0]
是否为false
int tmp = arr[0];if (tmp != 0) {
if (last_part)printf(" ");cout << num2str(tmp) << " " << "Yi";last_part = true;}
第8位
无特别注意点
tmp = arr[1];if (tmp != 0) {
if (last_part)printf(" ");cout << num2str(tmp) << " " << "Qian";last_part = true;}
第7位
本位非零,如果上一位为0,且亿位非零,要输出一个"ling "
// handle 7 digittmp = arr[2];if (tmp != 0) {
if (last_part)printf(" ");if (flag[0] && !flag[1])printf("ling ");cout << num2str(tmp) << " " << "Bai";last_part = true;}
第6位
本位非零时,如果上一位为0,且上上位之前存在至少一个非零,要输出一个"ling "
// handle 6 digittmp = arr[3];if (tmp != 0) {
if (last_part)printf(" ");if (!flag[2] && (flag[0] || flag[1]))printf("ling ");cout << num2str(tmp) << " " << "Shi";last_part = true;}
第5位
本位非零时,如果上一位为0,且上上位之前存在至少一个非零,要输出一个"ling "
// handle 5 digittmp = arr[4];if (tmp != 0) {
if (last_part)printf(" ");if (!flag[3] && (flag[0] || flag[1] || flag[2]))printf("ling ");cout << num2str(tmp);last_part = true;}
中间环节
p1z
若为true
,表示第8~5位全为0;p2z
若为true
,表示第4~1位全为0;
当亿位非零,第8到5位全为0,且第4到1位非全零(说明后面还有东西)时,要输出一个"ling "
当第8到5位非全零时,要输出" Wan"
// handle middlebool p1z = !flag[1] && !flag[2] && !flag[3] && !flag[4];bool p2z = !flag[5] && !flag[6] && !flag[7] && !flag[8];if (flag[0] && p1z && !p2z)printf(" ling");if (!p1z)printf(" Wan");
第4位
无注意点
// handle 4-1 digit// handle 4 digittmp = arr[5];if (tmp != 0) {
if (last_part)printf(" ");cout << num2str(tmp) << " " << "Qian";last_part = true;}
第3位
若本位非零,上一位为0,第8到5位非全零时,要输出一个"ling "
// handle 3 digittmp = arr[6];if (tmp != 0) {
if (last_part)printf(" ");if (!p1z && !flag[5])printf("ling ");cout << num2str(tmp) << " " << "Bai";last_part = true;}
第2位
若本位非零,上一位为0,第8到4位非全零时,要输出一个"ling "
// handle 2 digittmp = arr[7];if (tmp != 0) {
if (last_part)printf(" ");if ((!p1z || flag[5]) && !flag[6])printf("ling ");cout << num2str(tmp) << " " << "Shi";last_part = true;}
第一位
若本位非零,上一位为0,第8到3位非全零时,要输出一个"ling "
// handle 1 digittmp = arr[8];if (tmp != 0) {
if (last_part)printf(" ");if ((!p1z || flag[5] || flag[6]) && !flag[7])printf("ling ");cout << num2str(tmp);last_part = true;}
最后,若这个数本身就为0,则前面的过程都不会输出,在此直接做输出
if (num == 0) {
printf("ling");}
——END——
完整代码
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<map>
#include<set>
#include<queue>
#include<string.h>using namespace std;string num2str(int x) {
switch (x) {
case 0: return "ling";case 1: return "yi";case 2: return "er";case 3: return "san";case 4: return "si";case 5: return "wu";case 6: return "liu";case 7: return "qi";case 8: return "ba";default: return "jiu";}
}int main() {
string str;bool last_part = false;bool flag[9] = {
false };int arr[9];cin >> str;bool isFu = false;if (str[0] == '-') {
str.erase(0, 1);isFu = true;}// fill 9 digitsif (str.size() < 9)str.insert(0, 9 - str.size(), '0');for (int i = 0; i < 9; i++) {
arr[i] = str[i] - '0';if (arr[i] != 0)flag[i] = true;}// handle Fulong int num = stoi(str);if (num != 0 && isFu) {
printf("Fu");last_part = true;}// handle 9 digitint tmp = arr[0];if (tmp != 0) {
if (last_part)printf(" ");cout << num2str(tmp) << " " << "Yi";last_part = true;}// handle 8-5 digit// handle 8 digittmp = arr[1];if (tmp != 0) {
if (last_part)printf(" ");cout << num2str(tmp) << " " << "Qian";last_part = true;}// handle 7 digittmp = arr[2];if (tmp != 0) {
if (last_part)printf(" ");if (flag[0] && !flag[1])printf("ling ");cout << num2str(tmp) << " " << "Bai";last_part = true;}// handle 6 digittmp = arr[3];if (tmp != 0) {
if (last_part)printf(" ");if (!flag[2] && (flag[0] || flag[1]))printf("ling ");cout << num2str(tmp) << " " << "Shi";last_part = true;}// handle 5 digittmp = arr[4];if (tmp != 0) {
if (last_part)printf(" ");if (!flag[3] && (flag[0] || flag[1] || flag[2]))printf("ling ");cout << num2str(tmp);last_part = true;}// handle middlebool p1z = !flag[1] && !flag[2] && !flag[3] && !flag[4];bool p2z = !flag[5] && !flag[6] && !flag[7] && !flag[8];if (flag[0] && p1z && !p2z)printf(" ling");if (!p1z)printf(" Wan");// handle 4-1 digit// handle 4 digittmp = arr[5];if (tmp != 0) {
if (last_part)printf(" ");cout << num2str(tmp) << " " << "Qian";last_part = true;}// handle 3 digittmp = arr[6];if (tmp != 0) {
if (last_part)printf(" ");if (!p1z && !flag[5])printf("ling ");cout << num2str(tmp) << " " << "Bai";last_part = true;}// handle 2 digittmp = arr[7];if (tmp != 0) {
if (last_part)printf(" ");if ((!p1z || flag[5]) && !flag[6])printf("ling ");cout << num2str(tmp) << " " << "Shi";last_part = true;}// handle 1 digittmp = arr[8];if (tmp != 0) {
if (last_part)printf(" ");if ((!p1z || flag[5] || flag[6]) && !flag[7])printf("ling ");cout << num2str(tmp);last_part = true;}if (num == 0) {
printf("ling");}return 0;
}