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. Consul集群Server模式

    Consul集群Server模式 架构示意图 Consul在生产环境下运行模式分为两种:Server模式和Client模式(dev模式属于开发模式不在这里讨论),我们先用Server模式搭建一个Con ...

  2. 《剑指offer》面试题14 调整数组顺序使奇数位于偶数前面 Java版

    (输入整数数组,使所有奇数位于前半部分,所有偶数位于后半部分.) 我的方法:想到用两个下标分别表示奇数和偶数的界线,一个在开头,一个在末尾,判断每一个数字的类别,然后将它放入对应的范围内,移动下标,直 ...

  3. python抓取往期双色球

    import requests import json,csv ''' http://m.zhcw.com/clienth5.do?lottery=FC_SSQ&pageSize=20& ...

  4. RMAN备份与恢复 —— 参数文件还原

         在RMAN用语中,“还原”与“恢复”具有不同的含义,还原(restore)是指访问先前生成的备份集,从中得到一个或多个对象,然后再磁盘上的某个位置还原这些对象.还原与恢复时分离的.恢复(re ...

  5. Ribbon远程调用

    Ribbon是客户端的负载均衡机制,它有几种负载均衡机制.默认是轮询,我们也可以自定义规则.通过合理的分配网络请求来减小服务器的压力.项目都是注册到eureka服务器上.通过ribbon去调用其他服务 ...

  6. 剑指offer学习读书笔记--二维数组中的查找

    在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都是按照从上到下递增的顺序排序.请设计一个函数,输入这样的一个二维数组和一个整数,判断数组是否含有这个整数. 1 2 8 9 2 4 9 1 ...

  7. mui实现分页上拉加载更多 下拉刷新数据的简单实现 移动端下拉上拉

    空下来把mui上拉加载更多,下拉刷新数据做了一个简单的实现,希望可以帮助到需要的朋友 demo项目的结构 <!DOCTYPE html> <html> <head> ...

  8. python 安装 pip ,并使用pip 安装 filetype

    闲话少说,直接上操作. python版本为2.7.6 可以直接到官网下载,我也提供一个百度云的下载地址 https://pan.baidu.com/s/1kWPXG8Z 这个是window版本,lin ...

  9. openstack stein部署手册 5. placement

    # 建立数据库用户及权限 create database placement; grant all privileges on placement.* to placement@'localhost' ...

  10. Q开头的类找不到,无法加载插件:com.mysema.maven:apt-maven-plugin

    http://www.jspxcms.com/documentation/297.html 如果出现无法加载com.mysema.maven:apt-maven-plugin插件的情况,通常是由于ma ...