您的位置:首页 > 其它

华为oj 人民币转换

2016-03-09 17:33 363 查看


测试发现我的程序还有bug,不过可以通过oj。。。

#include<iostream>
#include<string>
using namespace std;
int main()
{
string tran1[10] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
string tran2[5] = { "拾", "佰", "仟", "万", "亿" };
string in, tin = "", result = "元";
int jiao = 0, fen = 0;

cin >> in;
int count = 0;
bool flag = false;
for (int i = 0; i < in.size(); i++)
{
if (in[i] != '.'&&!flag)
{
tin += in[i];
}
else
{
flag = true;
if (count == 1)
{
jiao = in[i] - '0';
}
else if (count == 2)
{
fen = in[i] - '0';
}
count++;
}
}

if (tin[tin.size() - 1] != '0')
{
result = tran1[tin[tin.size() - 1] - '0'] + result;
}

int count1 = 0;
for (int i = tin.size() - 2; i >= 0; i--)
{
if (tin[i] - '0' != 0)
{
if (count1 == 8)
{
result = tran2[4] + result;
}
else
{
result = tran2[count1 % 4] + result;
}

if (tin[i] - '0' != 1 || count1 % 4 != 0)
{
result = tran1[tin[i] - '0'] + result;
}
}

count1++;
count1 %= 9;
}

//输出
cout << "人民币" << result;
if (!jiao&&!fen)
{
cout << "整" << endl;
}
else
{
if (jiao)
{
cout << tran1[jiao] << "角";
}
if (fen)
{
cout << tran1[fen] << "分";
}
cout << endl;
}
return 0;
}
结论:这道题很复杂,很复杂,很复杂!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: