【21.67%】【codeforces 727B】Bill Total Value
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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
【题解】
一开始的时候一直在纳闷样例1是啥。
看了挺久还剩一个半小时,就先去做T3了,A了T3之后再回过头来看(本来是想说题目错了会改,但是并没有等到);
然后才看到。如果dolar那一位有三个以上的数字需要用点号分割.
12.123指的是12123美元
而
12.12指的是12美元12美分
然后就累加和呗。找到每个数字串里面最后一个点号的位置(如果存在);
然后看一下那个点号后面有几个数字,如果有3个,则整个字串全是dolar位,否则有两位小数(表示美分),注意最多只有两位小数!;
最后写到输出答案的部分的时候比赛时间只剩5分钟。
我想说肯定够了。
突然发现自己不会输出double x = 0.32的小数部分;
怎么把点号去掉呢?
我想着乘10,然后取整,再减去整数,再乘10再取整!(但是竟然爆精度了,玄学);
最后比赛完的那一瞬间猛然想到直接乘100再去整数部分不就好了!
等终测完了,代码把乘100的代码交上去,A了。哎万念俱灰。如果····,可惜没有如果。
当然不能直接取整数部分,如果小于10要再补个0;
哎,TAT掉了24分。明天还要上高数辅导班,得早退了。不然不能打DIV2了。我梦寐以求的重归蓝名计划(没错我蓝过!!)离我越来越远。
都怪我一直在捣鼓T3的交互是啥玩意0 0MLGB,现在想想要是T3知道交互输入是什么就能节省很多时间了。说不定这题就A了,啊啊啊啊啊啊啊啊啊!!
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
string s;
double ans = 0;
int main()
{
// freopen("F:\\rush.txt", "r", stdin);
cin >> s;
int len = s.size();
int now = 0;
while (now <= len - 1)//这整个while用于获取字符串里面包含的数字的和
{
int j = now + 1;
if (isdigit(s[now]))
{
double num = 0;
while (j <= len-1 && isdigit(s[j]) || s[j] == '.')
j++;
int t = j-now;
string s1 = s.substr(now, t);
int len1 = s1.size();
int pd = -1;
for (int i = len1-1;i >= 0;i--)
if (s1[i] == '.')
{
pd = i;//pd是这个数字len1里面最后一个点号的位置
break;
}
int size = len1-1 - pd;//这个点号后面的数字个数
if (pd==-1 || size > 2)//全是dolars
{
for (int i = 0; i <= len1 - 1; i++)
if (isdigit(s1[i]))
num = num * 10 + (s1[i] - '0');
ans += num;//累加答案
}
else//有美分在后面的情况
{
for (int i = 0; i <= pd - 1; i++)
if (isdigit(s1[i]))
num = num * 10 + (s1[i] - '0');
double tt = 1;
for (int i = pd + 1; i <= len1 - 1; i++)
{//小数部分
tt = tt*0.1;
num += (s1[i] - '0')*tt;
}
ans += num;//累加答案
}
}
now = j;
}
double temp = int(ans);
int dd = int(ans);
int no = 0;
string ss = "";//输出的时候美元也要3位一点号,有点麻烦所以用字符串吧
while (dd > 0)
{
int key = dd % 10;
char ke = key + '0';
ss = ke + ss;
no++;
dd = dd / 10;
if (no == 3)
{
no = 0;
if (dd >0)
ss = '.' + ss;
}
}
if (abs(ans - temp) <= 0.001)//如果没有小数部分
{
cout << ss << endl;
}
else//有小数部分
{
if (ss == "")
printf("0");
else
cout << ss;
printf(".");
double tt = ans - temp;
tt = tt * 100;//乘100···,说多了都是泪
if (tt < 10)
printf("0");
printf("%.0lf\n", tt);
}
return 0;
}
【21.67%】【codeforces 727B】Bill Total Value的更多相关文章
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【24.67%】【codeforces 551C】 GukiZ hates Boxes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【21.37%】【codeforces 579D】"Or" Game
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【26.67%】【codeforces 596C】Wilbur and Points
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【21.21%】【codeforces round 382D】Taxes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【21.58%】【codeforces 746D】Green and Black Tea
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 757D】Felicity's Big Secret Revealed
[题目链接]:http://codeforces.com/problemset/problem/757/D [题意] 给你一个01串; 让你分割这个01串; 要求2切..n+1切; 对于每一种切法 所 ...
- 【codeforces 757E】Bash Plays with Functions
[题目链接]:http://codeforces.com/problemset/problem/757/E [题意] 给你q个询问; 每个询问包含r和n; 让你输出f[r][n]; 这里f[0][n] ...
- 【codeforces 67A】Partial Teacher
[题目链接]:http://codeforces.com/problemset/problem/67/A [题意] 给一个长度为n-1的字符串; 每个字符串是'L','R','='这3种字符中的一个; ...
随机推荐
- hadoop组件及其作用
1.hadoop有三个主要的核心组件:HDFS(分布式文件存储).MAPREDUCE(分布式的计算).YARN(资源调度),现在云计算包括大数据和虚拟化进行支撑. 在HADOOP(hdfs.MAPRE ...
- Library Component Properties的表格如何填写
- trunc与round
TRUNC(number[,num_digits]) number 需要截尾取整的数字. num_digits 用于指定取整精度的数字.Num_digits 的默认值为 0. 作用:截断数字和时间 ...
- FTP中的授权规则
在授权规则中,你可以管理自己的FTP站点以怎样的方式进行访问,比如每个进入站点的人都需要输入用户名密码.正则可以在授权规则中删除默认的配置“允许匿名用户读取”的规则. 也可以在此处,对不同的组或用户进 ...
- thinkphp模型事件(钩子函数:模型中在增删改等操作前后自动执行的事件)
thinkphp模型事件(钩子函数:模型中在增删改等操作前后自动执行的事件) 一.总结 1.通过模型事件(钩子函数),可以在插入更新删除等前后执行一些特定的功能 2.模型事件是写在模型里面的,控制器中 ...
- DMA 如何查看它有没有传输完成 传输完成再开启
摘自:https://blog.csdn.net/zzwdkxx/article/details/9026173 /////////////////////////////////////////// ...
- 美国汪利宏的蒙特卡洛及卷积模拟程序,可以模拟top-hat光束和高斯光束在生物组织中的传输
链接:https://pan.baidu.com/s/1yaCsQ8TCVPSIZ4TVBZgfnw 密码:otzr
- Cannot find module "element-ui/lib/theme-default/index.css"——解决办法
在vuejs中使用element-ui 1.安装 cnpm install style-loader --save-dev cnpm install css-loader --save-dev cnp ...
- php实现求字符串第一个只出现一次的字符
php实现求字符串第一个只出现一次的字符 一.总结 很简单的逻辑 1.两个数组,一个存字母,一个存字母出现的次数 二.php实现求字符串第一个只出现一次的字符 题目描述 在一个字符串(1<=字符 ...
- 前端js实现打印excel表格
产品原型: 图片.png 功能需求:点击导出考勤表格按钮,会自动下载成Excel格式 图片.png 图片.png jsp页面代码: <div class="tools"> ...