题目:

A - Tutor

Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u

Description

Lilin was a student of Tonghua Normal University. She is studying at University of Chicago now. Besides studying, she worked as a tutor teaching Chinese to Americans. So, she can earn some money per month. At the end of the year, Lilin wants to know his average monthly money to decide whether continue or not. But she is not good at calculation, so she ask for your help. Please write a program to help Lilin to calculate the average money her earned per month.
 

Input

The first line contain one integer T, means the total number of cases. 
Every case will be twelve lines. Each line will contain the money she earned per month. Each number will be positive and displayed to the penny. No dollar sign will be included.
 

Output

The output will be a single number, the average of money she earned for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign without tail zero. There will be no other spaces or characters in the output.
 

Sample Input

2 100.00 489.12 12454.12 1234.10 823.05 109.20 5.27 1542.25 839.18 83.99 1295.01 1.75 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00
 

Sample Output

$1581.42
$100
分析:整体来说题目理解相当容易,只要是将题目意思搞定就可以敲出一行得到结果的代码。但是在处理四舍五入的问题时总是没法好好的搞定。而且这也不是第一次遇见这类东西了。现在先上关键代码后分析。

 #include <iostream>
#include <cstdio> using namespace std; int main(){
int t;
double a;
cin>>t;
while(t--){
double sum = ;
for(int i = ;i < ; i++){
cin>>a;
sum+=a;
}
sum/=;
int zhangjie = (int)(sum * );
zhangjie+=;
zhangjie/=;
sum=zhangjie/100.0;
if((int)(sum*)%!=)
printf("$%.2lf\n",sum);
else if((int)(sum*)%!=)
printf("$%.1lf\n",sum);
else printf("$%.0lf\n",sum);
}
return ;
};
int zhangjie = (int)(sum * 1000);
zhangjie+=5;
zhangjie/=10;
运算的结果是要求保留到小数点后两位,然后当然它就是和小数点的后三位是相关连的。我们先将结果*1000后取整,相当于将后面所有的部分全部的给去掉。然后的加五是为了实现四舍五入的效果。最后的除法也是为了去掉最后一位。在这里最关键的一点就是要十分的清楚,int和除法这两种操作都是直接的将最后面的部分给去掉。

然后就是在实现浮点数四舍五入的过程中,有函数ceil和函数floor可以实现的,

比如这个:
int round(double x)
{
return (x - floor(x) >= 0.5) ? (int)ceil(x) : (int)floor(x);
}

double类型之四舍五入的更多相关文章

  1. C# Double类型 不四舍五入

    测试中发现Double类型需要#0.00 小数点精度为后2位,并且多余部分不需要四舍五入,直接截断 用字符串处理也可以,但是比较麻烦 这里给出一种思路: double a = 9999.999; a ...

  2. c语言double类型数据四舍五入

    借助math库的round函数 #include <math.h> double ext_round(double data, int precision) { , precision); ...

  3. 【0624课外作业】将一个double类型的小数,四舍五入保留两位小数

    package com.work0624; /** * 课外作业 *将一个double类型的小数,四舍五入保留两位小数 * @author L * */ import java.util.Scanne ...

  4. 19-6/24作业: 将一个double类型的小数,按照四舍五入保留两位小数

    ☞要求 将一个double类型的小数,按照四舍五入保留两位小数 ☞实现方式 1.获得一个double类型的小数 2.使用BigDecimal包的setScale进行操作 3.输出结果 ☞代码内容 pa ...

  5. 关于BigDecimal 和 double 类型保存金钱,以及精度问题,银行家舍入法

    1. BigDecimal 类型数据 的创建,构造函数 有 public BigDecimal(BigInteger intVal, long val, int scale, int prec); p ...

  6. java double类型保留两位小数4种方法【转】

    4种方法,都是四舍五入,例: import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberF ...

  7. Double 类型运算时的精度问题

    double 类型运算时的 计算的精度不高,常常会出现0.999999999999999这种情况,那么就须要用BigDecimal   它是java提供的用来高精度计算的工具类 以下是对这个类的一个包 ...

  8. Java中double类型的数据精确到小数点后两位

    Java中double类型的数据精确到小数点后两位 多余位四舍五入,四种方法 一: double f = 111231.5585;BigDecimal b = new BigDecimal(f); d ...

  9. 不要在精确计算中使用float和double类型

    http://blog.csdn.net/androiddevelop/article/details/8478879 一  问题描述 float和double类型不能用于精确计算,其主要目的是为了科 ...

随机推荐

  1. Tomcat7调优及JVM性能优化for Linux环境

    标签: tomcat7 jvm 性能 分享到: 出处:http://www.iteye.com 该优化针对Linux X86_X64环境 1. Tomcat优化其实就是对server.xml优化(开户 ...

  2. HeapSpray初窥(2014.12)

    注:环境是xp+ie8 1.HeapSpray简介 Windows的堆因为动态分配和释放的特点,其看起来是不连续(没有规律的),但是仍可以找到一定的规律:大量的连续分配会更倾向使用连续的地址,减少了碎 ...

  3. VMware 8安装苹果操作系统Mac OS X 10.7 Lion正式版

    今天介绍下VM8下安装Mac OS X 10.7  1.工具篇       下载Vmware Workstation 8.0正式版http://115.com/file/bhyk1l2u#       ...

  4. MySQL 不允许从远程访问的解决方法

    解决方法: 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 ...

  5. 第十五节,基本数据类型,元组tuple

    元组和列表的区别 元组和列表几乎是一样的 不一样的地方就是元组创建后元组的元素不可以修改,比如(添加,拓展,移除等修改功能,但是元组里的元素的元素是可以修改的) 基本操作: 索引 切片 循环 长度 包 ...

  6. Linux文件系统的目录结构

    Linux下的文件系统为树形结构,入口为/ 树形结构下的文件目录: 无论哪个版本的Linux系统,都有这些目录,这些目录应该是标准的.各个Linux发行版本会存在一些小小的差异,但总体来说,还是大体差 ...

  7. shell脚本学习(一)

    1.hbg@HWM:/$ su root密码:root@HWM:/# $表示普通用户, #表示管理员用户root.root是linux系统中权限最高的用户. 2.在bash中,每个变量的值都是字符串. ...

  8. implement a system call in minix

    http://www.papervisions.com/implementing-system-call-in-minix-os/

  9. centos7与centos6区别

    CentOS 7 vs CentOS 6的不同   (1)桌面系统[CentOS6] GNOME 2.x[CentOS7] GNOME 3.x(GNOME Shell)(2)文件系统[CentOS6] ...

  10. Toolbar Painter 工具条制作

    工具条制作工具(ToolBarPainter2013.exe),专为程序猿设计,界面开发必备.当用VC编程制作工具条时,需要为工具栏上每一个button添加图标,是一件极其繁琐的事情,该工具可利用已有 ...