题目:

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. Zookeeper 启动错误

    启动后日志如下 : 2016-09-14 05:51:19,449 [myid:1] - INFO [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:FastLeade ...

  2. .Net_把文件数据添加到数据库中(面试题)

    一个文本文件含有如下内容: 4580616022644994|3000|赵涛 4580616022645017|6000|张屹 4580616022645090|3200|郑欣夏 上述文件每行为一个转 ...

  3. 关于个人网站选择虚拟主机还是VPS服务器的讨论

    还记得当初才开始学习建站的时候,选择的第一款虚拟主机是全HTML的主机,那时候的虚拟主机还分为HTML或者是ASP,PHP的都很少,在国内接触的学习较多还是以ASP为主,PHP是最近几年才开始流行.如 ...

  4. hdu_5690_All X(找循环节)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5690 题意: Problem Description F(x, m)F(x,m) 代表一个全是由数字x ...

  5. LeetCode OJ 292.Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  6. 安装阿里云的php+mysql+nginx+vsftpd

    百度云search   sh-1.3.0-centos.zip

  7. AIR使用文件对象操作文件和目录

    文件对象是啥?文件对象(File对象)是在文件系统中指向文件或目录的指针.由于安全原因,只在AIR中可用. 文件对象能做啥? 获取特定目录,包括用户目录.用户文档目录.该应用程序启动的目录和程序目录 ...

  8. Head First - 01.策略模式(Strategy Pattern)

    策略模式定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 当你需要给朋友留下深刻印象,或是影响关键主管的决策时,请使用“这个”定义!  设计原则: 1.找出 ...

  9. vi 使用教程

    编辑一个文本文件是经常使用到的计算机操作.我们想做的大多数事情都需要使用某种文件编辑.文本编辑器会方便文件的创建和修改.编辑一个文本文件是经常使用到的计算机操作.我们想做的大多数事情都需要使用某种文件 ...

  10. PhotoShop纸张大小

    1*标准打印纸 A4:210mm*297mm A3: 420mm*297mm 一张全开纸切成多少份 大度16开:210mm*285mm(度:切的意思) 大度8开:420*285mm 2*传统印刷纸 A ...