您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: