code force B. Bill Total Value

3638 단어 문자열
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달러를 가리키며 정수 부분은 세 자리마다 한 점을 사이에 두고 작은 표 명세서의 모든 금액의 총계를 구한다.
AC 코드:
#include #include #include #include #include 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';}elsefor (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", a[len - 2], a[len - 1]);system("pause");return 0; }

좋은 웹페이지 즐겨찾기