1082. Read Number in Chinese (25)解题报告
2016-11-26 20:03
369 查看
注意如何处理零。
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <vector> const int yi = 100000000; const int wan = 10000; using namespace std; void convert(int num, vector<string> &str, bool &flag); char digit[10][10] = { "ling", "yi", "er","san","si","wu","liu","qi","ba","jiu" }; int main(void) { bool flag; long long num, result; int i; vector<string> str; scanf("%lld", &num); if (num) { flag = false; if (num < 0) { str.push_back("Fu"); num = -num; } result = num / yi; num %= yi; if (result > 0) { flag = true; str.push_back(string(digit[result])); str.push_back(string("Yi")); } convert(num / wan, str, flag); if (flag && num / wan) { str.push_back(string("Wan")); } num %= wan; convert(num, str, flag); } else { str.push_back(string(digit[0])); } i = 0; cout << str[0]; for (i = 1 ; i < str.size(); i++) { cout << ' ' << str[i]; } cout << endl; return 0; } void convert(int num, vector<string> &str, bool &flag) { const int qian = 1000; const int bai = 100; const int shi = 10; int result; bool zero = false; result = num / qian; if (result) { flag = true; str.push_back(string(digit[result])); str.push_back(string("Qian")); } else { zero = true; } num %= qian; result = num / bai; if (result) { if (zero && flag) { str.push_back(string(digit[0])); zero = false; } if (!flag) { flag = true; zero = false; } str.push_back(string(digit[result])); str.push_back(string("Bai")); } else { zero = true; } num %= bai; result = num / shi; if (result) { if (zero && flag) { str.push_back(string(digit[0])); zero = false; } if (!flag) { flag = true; zero = false; } str.push_back(string(digit[result])); str.push_back(string("Shi")); } else { zero = true; } num %= shi; result = num; if (result) { if (zero && flag) { str.push_back(string(digit[0])); zero = false; } if (!flag) { flag = true; zero = false; } str.push_back(string(digit[result])); } return; }
相关文章推荐
- 剑指offer解题报告(Java版)——二叉树中和为某一值的路径 25
- 1062. Talent and Virtue (25)解题报告
- 1079. Total Sales of Supply Chain (25)解题报告
- 1089. Insert or Merge (25)解题报告
- 1097. Deduplication on a Linked List (25)解题报告
- Family Property (25)解题报告
- PAT (Advanced Level) 1002. A+B for Polynomials (25) 解题报告
- COGS 25. [NOIP2007] 守望者的逃离 解题报告
- 1063. Set Similarity (25)解题报告
- 1071. Speech Patterns (25)解题报告
- 1074. Reversing Linked List (25)解题报告
- 1085. Perfect Sequence (25)解题报告
- 1098. Insertion or Heap Sort (25)解题报告
- 1106. Lowest Price in Supply Chain (25)解题报告
- PAT (Advanced Level) 1003. Emergency (25) 解题报告
- CDOJ 25 点球大战(penalty) 解题报告
- 1083. List Grades (25)解题报告
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
- [BestCoder Round #25 1002]Harry And Magic Box 解题报告
- 1066. Root of AVL Tree (25)解题报告