正整数的汉语读法
来源:互联网 发布:写一个数组 编辑:程序博客网 时间:2024/04/30 15:32
题目描述:现在让你在普通的计算器(位数最多12位)上增加一个功能button,点击这个button之后能进行数字显示模式的切换,即能显示我们汉语对数字表达的习惯。比如计算器最初显示“零”,你按“1”这个按键之后,屏幕显示的是“一”,继续按”9”这个键,显示的是”十九”,继续按“0”这个键,显示的是“一百九十”… …而且,还要增加两个功能button,“退格键”和“清空键”。继续按退格键,显示的是“十九”,继续按“清空建”,显示的是“零”。
正确代码置顶:
------------------------------------------------------------------------------------
如果位数很大,兆以上的单位可以用getBase函数来获得,那么代码可以这么写:
1. 分段,如果不存在0,在len-i ==5 , == 9, == 13的位置输出万,亿,兆,其他位置输出数字+十百千即可
2. 如果存在0,遍历这段连续的0,如果中间经历len-i == 13, 9, 5的位置,输出相应的第一个单位兆>亿>万,之后break
·这里得强调一下: 当前一个非0输出一个base后,这个时候连续4个0是不用被输出的!!!!所以有了(k-i)%4 != 3
3. 如果0是末位,中间不用加0,否则要加一个0
#include <stdio.h>#include <string>#include <string.h>#include <iostream>using namespace std;string getBase(int num) { if (num == 5) return "万"; else if (num == 9) return "亿"; else if (num == 13) return "兆"; else return "";}Another version:int main() { string input = "101030100105"; string res = ""; string num[] = {"零","一","二","三","四","五","六","七","八","九"}; string unit[] = {"千","","十","百"}; int len = input.length(), i, j; for (i = 0; i < len; ) { if (input[i] != '0') { res += num[input[i]-'0']; res += unit[(len-i)%4]; res += getBase(len-i); ++i; } else { int j = i; while (j < len && input[j] == '0') ++j; for (int k = i; k < j; ++k) if (getBase(len-k) != "" && (k-i)%4 != 3) { res += getBase(len-k); break; } if (j != len) res += "零"; i = j; } } return 0;}
-------------------------------------------------------------------------------------
注意连续的0的位置即可:
#include <stdio.h>#include <string>#include <iostream>using namespace std;string print(const string& num) { string str[] = {"零","一","二","三","四","五","六","七","八","九","十","百","千"}; string unit[] = {"十","百","千"}; string res = ""; int i, j, len = num.length(); for (i = 0; i < len; ++i) { if (num[i] == '0') { for (j = i + 1; num[j] == '0' && j < len; ++j); if (len - (i - 1) >= 6 && len - j <= 4) res += "万"; if (j != len) res += "零"; i = j - 1; continue; } else { res += str[num[i] - '0']; //cout << (len - i) % 5 << endl; // 2 3 4 6 7 8 if ((len - i) % 4 != 1) res += unit[(len - i - 2) % 4]; } if (len - i == 5) res += "万"; if (len - i == 9) res += "亿"; } return res;}int main() { string s = "1010"; cout << print(s) <<endl; return 0;}
如果位数很大,兆以上的单位可以用getBase函数来获得,那么代码可以这么写:
1. 分段,如果不存在0,在len-i ==5 , == 9, == 13的位置输出万,亿,兆,其他位置输出数字+十百千即可
2. 如果存在0,遍历这段连续的0,如果中间经历len-i == 13, 9, 5的位置,输出相应的第一个单位兆>亿>万,之后break
3. 如果0是末位,中间不用加0,否则要加一个0
把握这三点即可。。。
#include <stdio.h>#include <string>#include <iostream>using namespace std;string getBase(int i) { if (i == 5) return "万"; else if (i == 9) return "亿"; else if (i == 13) return "兆"; else return "";}string print(const string& num) { string str[] = {"零","一","二","三","四","五","六","七","八","九"}; string unit[] = {"十","百","千"}; string res = ""; int i, j, len = num.length(); for (i = 0; i < len; ++i) { if (num[i] == '0') { for (j = i + 1; num[j] == '0' && j < len; ++j); for (int k = len - i; k >= len - j; --k) { string tmp = getBase(k); if (tmp != "") { res += tmp; break; } } if (j != len) res += "零"; i = j - 1; continue; } else { res += str[num[i] - '0']; //cout << (len - i) % 5 << endl; // 2 3 4 6 7 8 if ((len - i) % 4 != 1) res += unit[(len - i - 2) % 4]; } res += getBase(len - i); } return res;}int main() { string s = "101030100105"; cout << print(s) <<endl; return 0;}
#include <stdio.h>#include <string>#include <string.h>#include <iostream>using namespace std;string getBase(int num) { if (num == 5) return "万"; else if (num == 9) return "亿"; else if (num == 13) return "兆"; else return "";}Another version:int main() { string input = "101030100105"; string res = ""; string num[] = {"零","一","二","三","四","五","六","七","八","九"}; string unit[] = {"千","","十","百"}; int len = input.length(), i, j; for (i = 0; i < len; ) { if (input[i] != '0') { res += num[input[i]-'0']; res += unit[(len-i)%4]; res += getBase(len-i); ++i; } else { int j = i; while (j < len && input[j] == '0') ++j; for (int k = i; k < j; ++k) if (getBase(len-k) != "" && (k-i)%4 != 3) { res += getBase(len-k); break; } if (j != len) res += "零"; i = j; } } return 0;}
#include <stdio.h>#include <string>#include <string.h>#include <iostream>using namespace std;string getBase(int num) { if (num == 5) return "万"; else if (num == 9) return "亿"; else if (num == 13) return "兆"; else return "";}Another version:int main() { string input = "101030100105"; string res = ""; string num[] = {"零","一","二","三","四","五","六","七","八","九"}; string unit[] = {"千","","十","百"}; int len = input.length(), i, j; for (i = 0; i < len; ) { if (input[i] != '0') { res += num[input[i]-'0']; res += unit[(len-i)%4]; res += getBase(len-i); ++i; } else { int j = i; while (j < len && input[j] == '0') ++j; for (int k = i; k < j; ++k) if (getBase(len-k) != "" && (k-i)%4 != 3) { res += getBase(len-k); break; } if (j != len) res += "零"; i = j; } } return 0;}
- 正整数的汉语读法
- 正整数的中文读法(C++ Python)
- 数字转汉语读法的方法设计(C#)
- Java实现读数字的问题,输入长整型数字,输出汉语里面的读法
- 输出以个小于1万亿的正整数的拼音读法
- 希腊字母的读法
- 日语标点的读法
- 英语标点的读法
- 所有希腊字母的读法
- 数学符号的读法
- const pointer的读法
- 数学公式的英语读法
- 英文标点符号的读法
- 源码的读法
- BIOS的读法
- 英语数字的读法
- 希腊字母的读法
- 符号的英文读法
- Remove Element
- C++程序员安装Eclipse
- 二叉树的非递归遍历
- 假如我有双翅膀
- 一般软件开发流程和一般软件的测试原则
- 正整数的汉语读法
- 最大子数组java实现
- LeetCode Search in Rotated Sorted Array
- 背包问题—杭电2602 Bone Collector
- 编译pf_ring下的ixgbe驱动出错
- algorithm头文件里的函数
- LeetCode Search for a Range
- 优先队列---priority_queue
- 排序总结之插入式排序