code force B. Bill Total Value

本文介绍了一种用于计算购物小票上商品总价的算法,该算法能够正确处理各种格式的价格表示,包括美元和美分,并能准确计算出总价。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


B. 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


题意:48.32指的是48美元32美分(后面两位小数) 12.390指的是12390美元(点后三位),整数部分每隔三位隔一个点,求出小票单子中所有金额的总和。


AC代码:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define rep(i,m,n) for(i=m;i<=n;i++)
int n, m, k, t;
char a[100005];
double sum, zs, xs;
int main()
{
int i, j;
scanf("%s", a);
int len = strlen(a);
a[len] = 'a';
rep(i, 0, len)
{
if (isdigit(a[i]))
{
j = i;
int cnt = 0;
xs = 0; zs = 0;
while (isdigit(a[j]) || a[j] == '.')j++;
while (a[j - 1] != '.'&&j - 1 > i)cnt++, j--;
if (cnt == 2 && j - 1 > i)
{
xs += 0.1*(a[j] - '0') + 0.01*(a[j + 1] - '0');
rep(k, i, j - 1) if (isdigit(a[k])) zs = zs * 10 + a[k] - '0';
}
else if (cnt == 3 && j - 1 > i)
{
rep(k, i, j + 2) if (isdigit(a[k])) zs = zs * 10 + a[k] - '0';
}
else
for (k = i; isdigit(a[k]); k++) zs = zs * 10 + a[k] - '0';
zs += xs; sum += zs;
while (isdigit(a[j]))j++;
i = j;
}
}
sprintf(a, "%.2f", sum);
len = strlen(a);
rep(i, 0, len - 4)
{
printf("%c", a[i]);
if (len - 4 - i>0 && (len - 4 - i) % 3 == 0) printf(".");
}
if (a[len - 1]>'0' || a[len - 2]>'0')printf(".%c%c\n", a[len - 2], a[len - 1]);
system("pause");
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值