zoj2476

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值