- 有几点需要注意的地方一是将right转化为与left在在同一节
while (left + 4 <= right){ right -= 4;//每次将right移动4位,直到left与right在同一节}
参考代码:
#define _CRT_SECURE_NO_WARNINGS#include #include #include char num[10][5] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };char wei[5][5] = { "Shi","Bai","Qian","Wan","Yi" };int main(){ char str[15]; scanf("%s", str);//按字符串输入数字 int len = strlen(str); int left = 0, right = len - 1;//分别指向字符串的首尾元素 if (str[0] == '-') { printf("Fu"); left++; } while (left + 4 <= right) { right -= 4;//每次将right移动4位,直到left与right在同一节 } while (left < len) { bool flag = false;//flag==false表示没有累计的0 while (left <= right) { if (left > 0 && str[left] == '0') { flag = true; } else { if (flag == true) { printf(" ling"); flag = false; } if (left > 0) printf(" ");//只要不是首位都要在每个后面输出空格 printf("%s", num[str[left] - '0']);//输出当前位数字 if (left != right) { printf(" %s", wei[right - left - 1]); } } left++; } if (right != len - 1) { printf(" %s", wei[(len - 1 - right) / 4 + 2]); } right += 4; } system("pause"); return 0;}