Problem Description
Avin’s company has many ongoing projects with different budgets. His company records the budgets using numbers rounded to 3 digits after the decimal place. However, the company is updating the system and all budgets will be rounded to 2 digits after the decimal place. For example, 1.004 will be rounded down
to 1.00 while 1.995 will be rounded up to 2.00. Avin wants to
know the difference of the total budget caused by the update.
 
Input
The first line contains an integer n (1 ≤ n ≤ 1, 000).
The second line contains n decimals, and the i-th decimal ai (0 ≤ ai ≤ 1e18)
represents the budget of the i -th project. All decimals are rounded to 3
digits.
 
Output
Print the difference rounded to 3 digits..
 
Sample Input
1
1.001
1
0.999
2
1.001 0.999
 
Sample Output
-0.001
0.001
0.000
 
中文题意:给定一个数n,然后接下来给n个小数(小数点后都是三位),问你将这些小数四舍五入到两位小数后的和减去原先小数的和的值(保留三位小数)
错误:刚开始,我是用double来接收每个小数,然后将其*1000赋值给一个整型变量a,再判断a%10>=5,看是舍是进位?但是我忘记了小数的范围,如果将其乘以1000之后就算是long long int 也存不下,所以wa了好几次
思路:需要的结果是四舍五入为两位小数的和减去原先三位小数的和,所以这个差值其实是只与输入的小数的小数部分有关,所以我完全可以利用字符串只接受小数的最后三位,然后再对其进行操作,至于再往后就是最基本的知识了
AC代码:

#include<iostream>
using namespace std;
int main(){
int n,a;
double s1=0,s2=0,val;
char c;
cin>>n;
for(int i=0;i<n;i++){
while(1){
cin>>c;
if(c=='.') break;
}
a=0;
for(int j=0;j<3;j++){
cin>>c;
a=a*10+c-'0';
}
val=a;
val/=1000;
s1+=val;
if(a%10>=5){
a=a/10;
a++;
}
else
a=a/10;
val=a;
val/=100;
s2+=val;

}
printf("%.3f\n",s2-s1);
return 0;
}

hdu6575Budget的更多相关文章

随机推荐

  1. 生产者消费者模型(JoinableQueue)

  2. QT中使用Event Filter监听button事件,Release后button不见

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhenyu5211314/article/details/27201043 问题RT,在程序中我使用 ...

  3. .NET Core _linux sdk安装

    根据官方介绍页面的步骤: 步骤1. sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/do ...

  4. v-cloak解决Vue双大括号闪烁问题

    相信不少人和我一样,初次查看一个技术的文档的时候,知识吸收的很慢,因为对这个技术的不熟悉导致不清楚各种操作的应用场景,当我意识到这件事之后,我决定换种学习思路,即以实战为主,卡壳就查文档,会对这个技术 ...

  5. 调用SM30数据表维护的函数

    相关文章:http://www.cnblogs.com/caizjian/p/3248499.html 1.se11进去新建一个数据表 2.se55进去生产表维护 3.sm30进去维护数据表 4.se ...

  6. 2018-8-10-C#-ValueTuple-原理

    title author date CreateTime categories C# ValueTuple 原理 lindexi 2018-08-10 19:16:52 +0800 2018-2-13 ...

  7. 认知redis

    一.redis是什么? 1.基于key-value的内存No sql 数据库(非关系型数据库) 2.读写性能非常好 二.redisd的数据类型有哪些?特点分别是什么? 1)string 一个键对一个值 ...

  8. 【改】utf-8 的去掉BOM的方法

    最近在测试中发现,linux系统中导出的文件,有记事本打开另存为或者保存后,再次导入进linux系统,发现失败了,对比文件内容,没发现区别,打开二进制文件对比发现,文件头部多了三个字符:EF BB B ...

  9. 采集容器内存并写到excel

    # coding=utf-8 import os import commands import re from pyExcelerator import * def execute(cmd): sta ...

  10. bzoj4002 [JLOI2015]有意义的字符串 特征根+矩阵快速幂

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4002 题解 神仙题. 根据下面的一个提示: \[ b^2 \leq d \leq (b+1)^ ...