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:
Write a program that will find the total price of all purchases in the given bill.
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.
Print the total price exactly in the same format as prices given in the input.
chipsy48.32televizor12.390
12.438.32
a1b2c3.38
6.38
aa0.01t0.03
0.04
Source
Technocup 2017 - Elimination Round 1 (Unofficially Open for Everyone, Rated for Div. 2)
My Solution
题意:每一个物品有自己的价格,求价格总数,并按照要求的格式输出,小数只有2为,且整数部分每3位用一个小数点来分隔。
表达式处理、sstream
全部转化为乘100以后转化为整数加法,(这个过程可能有精度损失,所以addup = (add + 0.005) * 100;
然后最好 sum % 100就是小数部分(可能为0),而sum / 100 为整数部分(也可能为0)。
其中用sstream的stringstream来把字符串类型的数字转化成整型的数字
// It is guaranteed that each price in the bill is not less than one cent and not greater than 106 dollars.
/*
此外
对于stringstream的使用,如果没有改变转化类型则可用.clear()来重置,
如果再一次使用时要转化的类型,则用 .str("")来重置
*/
复杂度 O(n)
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const LL maxn = 1e6 + 8;
string s, t, v;
int main()
{
#ifdef LOCAL
freopen("b.txt", "r", stdin);
//freopen("b.out", "w", stdout);
LL T = 4;
while(T--){
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);
stringstream ss;
cin >> s;
LL sz = s.size(), sum = 0, addup, i, j, szt;
double add;
for(i = 0; i < sz; i++){
if(s[i] >= '0' && s[i] <= '9'){
t += s[i];
}
else if(s[i] == '.' && !t.empty()){
t += s[i];
}
else if(!t.empty()){
//cout << t << endl;
szt = t.size();
for(j = 0; j < szt - 3; j++){
if(t[j] >= '0' && t[j] <= '9'){
v += t[j];
}
}
for(j = max((LL)0, szt - 3); j < szt; j++){
v += t[j];
}
//cout << v << endl;
t.clear();
ss << v;
ss >> add;
addup = (add + 0.005) * 100;
//cout << addup << endl;
sum += addup;
ss.clear();
//如果没有改变转化类型则可用.clear()来重置,
v.clear();
}
}
if(!t.empty()){
//cout << t << endl;
szt = t.size();
for(j = 0; j < szt - 3; j++){
if(t[j] >= '0' && t[j] <= '9'){
v += t[j];
}
}
for(j = szt - 3; j < szt; j++){
v += t[j];
}
//cout << v << endl;
t.clear();
ss << v;
ss >> add;
//cout << add << endl;
addup = (add + 0.005) * 100; //!0.03 add * 100 变成了 2 ⊙﹏⊙‖∣,精度损失
//cout << addup << endl;
sum += addup;
ss.clear();
v.clear();
}
//cout << sum << endl;
LL b = sum % 100, a = sum / 100, cnt = 0;
ss.str(""); //如果再一次使用时要转化的类型,则用 .str("")来重置
t.clear();
//cout << a << endl;
ss << a;
ss >> t;
//cout << t << endl;
szt = t.size();
v.clear();
for(j = szt - 1; j >= 0; j--){
cnt++;
v = t[j] + v;
if(cnt == 3 && j != 0){
v = "." + v;
cnt = 0;
}
}
/*//test ss
while(a > 0){
cnt++;
sum = (a % 10);
char ch = sum + '0';
v = ch + v;
if(cnt == 3 && a / 10 != 0){
v = "." + v;
cnt = 0;
}
a /= 10;
}
*/
cout << v; //cout << b <
Thank you!
------from ProLights