CodeForces--TechnoCup--2016.10.15--ProblemB--Bill Total Value(字符串处理)
2016-10-16 14:42
423 查看
Bill Total Value
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Vasily exited from a store and now he wants to recheck the total price of all purchases in his bill. The bill is a string in which the names of the purchases and their prices are printed in a row without any spaces. Check has the format "name1price1name2price2...namenpricen", where namei (name of the i-th purchase) is a non-empty string of length not more than 10, consisting of lowercase English letters, and pricei (the price of the i-th purchase) is a non-empty string, consisting of digits and dots (decimal points). It is possible that purchases with equal names have different prices.
The price of each purchase is written in the following format. If the price is an integer number of dollars then cents are not written.
Otherwise, after the number of dollars a dot (decimal point) is written followed by cents in a two-digit format (if number of cents is between 1 and 9 inclusively, there is a leading zero).
Also, every three digits (from less significant to the most) in dollars are separated by dot (decimal point). No extra leading zeroes are allowed. The price always starts with a digit and ends with a digit.
For example:
"234", "1.544", "149.431.10", "0.99" and "123.05" are valid prices,
".333", "3.33.11", "12.00", ".33", "0.1234" and "1.2" are not valid.
Write a program that will find the total price of all purchases in the given bill.
Input
The only line of the input contains a non-empty string s with length not greater than 1000 — the content of the bill.
It is guaranteed that the bill meets the format described above. It is guaranteed that each price in the bill is not less than one cent and not greater than 106 dollars.
Output
Print the total price exactly in the same format as prices given in the input.
Examples
Input
chipsy48.32televizor12.390
Output
12.438.32
Input
a1b2c3.38
Output
6.38
Input
aa0.01t0.03 Output
0.04
这个题的难点在于字符串的处理,在翻看公开的代码时,我看到了一个写的特别棒的代码,充分运用了各种字符串有关的函数,感觉受益匪浅
#include "bits/stdc++.h" using namespace std; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll; template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; } template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; } int main() { string s; while(cin >> s) { for(char &c : s) if(isalpha(c)) c = ' '; stringstream ss(s); string w; ll sum = 0; while(ss >> w) { if(!(w.size() >= 4 && w[w.size() - 3] == '.')) w += ".00"; ll p = 0; for(char c : w) if(isdigit(c)) p = p * 10 + (c - '0'); //牛逼 sum += p; } char buf[100]; vector<string> v; if(sum % 100 != 0) { sprintf(buf, "%02d", (int)(sum % 100)); v.push_back(buf); } sum /= 100; while(sum >= 1000) { sprintf(buf, "%03d", (int)(sum % 1000)); v.push_back(buf); sum /= 1000; } v.push_back(to_string(sum)); //to_string() 函数将其他数据类型转化为string/***/http://www.cplusplus.com/reference/string/to_string/ reverse(v.begin(), v.end()); int i = 0; for(string s : v) { if(i ++ != 0) putchar('.'); printf("%s", s.c_str()); } puts(""); } return 0; }
相关文章推荐
- Technocup 2017 - Elimination Round 1 (Rated for Div. 2) B. Bill Total Value 表达式处理、sstream
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861C Did you mean...【字符串枚举,暴力】
- CodeForces--TechnoCup--2016.10.15--ProblemA--Transformation: from A to B
- 【21.67%】【codeforces 727B】Bill Total Value
- XVII Open Cup named after E.V. Pankratiev. Eastern Grand Prix. Problem G. Gmoogle 模拟、字符串处理、文本搜索
- B - Bill Total Value(小数点问题处理)
- codeforces 190C STL 字符串处理+栈思想
- CodeForces 1B Spreadsheets (字符串处理,注意细节,大胆尝试)
- CodeForces 4C Registration system(字符串处理 | map)
- Codeforces 448B (字符串简单处理,读题)
- CodeForces 589A(处理字符串)
- The Hardest Problem Ever【字符串处理 水题】
- 2015浙工大校赛-Problem D: IpOrUrl(字符串处理)
- CodeForces 159D 字符串处理
- codeforces 459D - Pashmak and Parmida's problem【离散化+处理+逆序对】
- codeforces_600A. Extract Numbers(字符串处理)
- Codeforces 558E 线段树处理字符串内排序
- Codeforces VK Cup 2012 Qualification Round 1 /158C (字符串处理)
- Codeforces 551B ZgukistringZ (字符串处理)
- codeforces 459D - Pashmak and Parmida's problem【离散化+处理+逆序对】