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. React中state和props分别是什么?

    整理一下React中关于state和props的知识点. 在任何应用中,数据都是必不可少的.我们需要直接的改变页面上一块的区域来使得视图的刷新,或者间接地改变其他地方的数据.React的数据是自顶向下 ...

  2. SCUT - 77 - 哈利波特与他的魔法杖

    https://scut.online/p/77 METO说是单点更新线段树.要记录哪些点不用再更新,不太清楚具体是要怎么实现? 一个类似的想法是把n个点建一棵平衡树,每次节点变成0之后从树上移除,至 ...

  3. HCL AppScan Standard 9.0.3.13

    https://pan.baidu.com/s/1mh97vyJdWy1CmF589jZJhQ 网盘密码: q31g / 压缩密码:shungg.cn http://www.shungg.cn/pos ...

  4. appscan 历史版本下载

    https://www.cnblogs.com/hua198/p/11120916.html

  5. P2517 [HAOI2010]订货(dp)

    P2517 [HAOI2010]订货 设$f[i][j]$表示第$i$个月,库存为$j$的最小代价 枚举上个月的库存$k$,那么$f[i][j]=f[i-1][k]+(j+U[i]-k)*D[i]+j ...

  6. 11-jQuery简介和选择器

    # jQuery > jQuery是一个是免费.开源的javascript库, 也是目前使用最广泛的javascript函数库.>> jQuery极大的方便你完成web前段的相关操作 ...

  7. Ubuntu 安装 ansible

    sudo apt update sudo apt-get install software-properties-common sudo apt-add-repository --yes ppa:an ...

  8. win32 socket 编程(三)——TCP/IP

    一.TCP/IP解析 TCP/IP协议的核心部分是传输层协议(TCP.UDP),网络层协议(IP)和物理接口层,这三层通常是在操作系统内核中实现.因此用户一般不涉及.编程时,编程界面有两种形式: 1. ...

  9. python学习笔记(10):面向对象

    一.类和实例 1.类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 2.对象:通过类定义的数据结构实例.对象包括两个数据成员( ...

  10. 一、Rabbit使用-安装教程

    首先我去官网上面下载RabbitMQ安装包:https://www.rabbitmq.com/which-erlang.html 现在下载的版本是3.7.17  因为我erlong安装的是20.3