#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
ifstream cin("2467.txt");
string s1, s2, temp;
int flag, a, b, sum;
int n;
map<char, int> m1;
m1['0'] = 0;
m1['1'] = 1;
m1['2'] = 2;
m1['3'] = 3;
m1['4'] = 4;
m1['5'] = 5;
m1['6'] = 6;
m1['7'] = 7;
m1['8'] = 8;
m1['9'] = 9;
map<int, char> m2;
m2[0] = '0';
m2[1] = '1';
m2[2] = '2';
m2[3] = '3';
m2[4] = '4';
m2[5] = '5';
m2[6] = '6';
m2[7] = '7';
m2[8] = '8';
m2[9] = '9';
while (cin >> n && n)
{
for (int i = 0; i < n; ++i)
{
temp = "";
cin >> s2;
//删除$号
s2.erase(0, 1);
//删除‘,’号
for (int j = 0; j < s2.size(); ++j)
{
if (s2[j] != ',')
temp += s2[j];
}
s2 = temp;
//翻转
reverse(s2.begin(), s2.end());
//删除'.'号
s2.erase(2, 1);
if (i == 0)s1 = s2;
else
{
//s1 + s2
sum = flag = 0;
if (s1.size() < s2.size())
{
temp = s1;
s1 = s2;
s2 = temp;
}
for (int k = 0; k < s1.size(); ++k)
{
a = m1[s1[k]];
if (k >= s2.size())b = 0;
else
b = m1[s2[k]];
sum = a + b + flag;
if (sum > 9)
{
sum = sum - 10;
flag = 1;
}
else
flag = 0;
s1[k] = m2[sum];
}
if (flag == 1)s1 += "1";
}
}
//加入小数点和逗号
temp = "";
for (int k = 0; k < s1.size(); ++k)
{
temp += s1[k];
if (k == 1)temp += ".";
if ((k - 1) % 3 == 0 && k != 1 && k != (s1.size() - 1))
temp += ",";
}
s1 = temp;
cout << "$";
for (int i = s1.size() - 1; i >= 0; --i)
cout << s1[i];
cout << endl;
}
return 0;
}
zoj2476
最新推荐文章于 2020-01-27 10:07:07 发布