首页 > 代码库 > PAT 1082. Read Number in Chinese
PAT 1082. Read Number in Chinese
#include <cstdio>#include <cstdlib>#include <string>#include <vector>#include <algorithm>using namespace std;const char* d2cn[] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};const char* t2cn[] = {"Ge", "Shi", "Bai", "Qian"};void num2cn(int num, vector<string>& out) { vector<string> tmp; int i = 0; int last = 0; while (num) { int cur = num % 10; if (cur != 0 || last != 0) { if (i != 0 && cur != 0) { tmp.push_back(string(t2cn[i])); } tmp.push_back(string(d2cn[cur])); } last = cur; num /= 10; i++; } reverse(tmp.begin(), tmp.end()); out.insert(out.end(), tmp.begin(), tmp.end());}int main() { vector<string> out; int N; scanf("%d", &N); int num = N; if (num < 0) { out.push_back("Fu"); num = -num; } int ds[9] = {0}; int i = 0; int yi = num / 100000000; num = num % 100000000; int wan = num / 10000; int ge = num % 10000; if (yi != 0) { out.push_back(d2cn[yi]); out.push_back("Yi"); } if (wan != 0) { if (wan < 1000 && yi != 0) { out.push_back("ling"); } num2cn(wan, out); out.push_back("Wan"); } if (ge != 0) { if (ge < 1000 && (yi | wan)) { out.push_back("ling"); } num2cn(ge, out); } if (out.size() == 0) { printf("ling"); } else { printf("%s", out[0].c_str()); int len = out.size(); for (int i=1; i<len; i++) { printf(" %s", out[i].c_str()); } } printf("\n"); return 0;}
坚持
PAT 1082. Read Number in Chinese
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。