虽然在网上看到很多大佬评价这题不难,但是个人感觉这道题目还是很难的
以下是在算法笔记中看到的解法。虽然这个解法在pta里面通过了,但是到牛客网上却通过不了:
测试用例:
100001234
对应输出应该为:
yi Yi ling yi Qian er Bai san Shi si
你的输出为:
yi Yi yi Qian er Bai san Shi si
确实存在一定问题,但是经过修改,反而又出现了新的问题,到现在也没想到好的解决方法,先放一下,等以后有新想法再修改,
#include<cstdio>
#include<cstring>
int main()
{
char num[10][10] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
char wei[5][10] = { "Shi","Bai","Qian","Wan","Yi" };
char str[15];
scanf("%s", str);
int len = strlen(str);
int left = 0;
int right = len - 1;
if (str[0] == '-')
{
printf("Fu");
left++;
}
while (left + 4 <= right)//使得right与left在同样的四位之内,要取到等号
{
right -= 4;
}
while (left < len)
{
bool flag = 0;//累积的0
bool isprint = 0;//是否输出
while (left <= right)
{
if (left > 0 && str[left] == '0')
{
flag = 1;
}
else
{
if (flag == 1)
{
printf(" ling");//留空格
flag = 0;
}
if (left > 0) printf(" ");//输出数的前面的空格
printf("%s", num[str[left] - '0']);
isprint = 1;
if(left!=right) printf(" %s", wei[right - left - 1]);//留空格
}
left++;
}
if (isprint == 1 && right != len - 1) printf(" %s", wei[(len - right - 1) / 4 + 2]);//留空格
right += 4;
}
return 0;
}