题目:

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);
}

最新文章

  1. [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World
  2. redis系列-redis的使用场景
  3. Jetty 9 源码分析 Connector及Server类(一)
  4. HDU 4778 Gems Fight!(DP)
  5. ACM 杂题,思路题 整理
  6. Jquery 前端模版
  7. 某硕笔试题mysql数据库部分(较为全面)
  8. delphi xe6 打开andoridGPS设置
  9. poj 3604 Professor Ben
  10. jmeter 使用聚合报告分析jtl文件
  11. 【动态规划】Codeforces 711C Coloring Trees
  12. Maven本地
  13. Maven搭建springMVC+spring+hibernate环境
  14. Ibatis 3.0 之前使用的都是2.0 3.0与2.0的内容有很大的不同
  15. AndroidManifest.xml文件
  16. Assignments
  17. OptaPlanner - 把example运行起来(运行并浅析Cloud balancing)
  18. cf571B Minimization (dp)
  19. Unity3D之物理射线
  20. Oracle表的查询(一)

热门文章

  1. 常用的meta标签
  2. Dash:程序员的好帮手
  3. iOS不可变数组的所有操作
  4. C语言_用if```else语句解决奖金发放问题
  5. vi 操作技巧
  6. MFC应用程序编写实例—完整版(原创)
  7. opencv 一堆算法,图像处理等
  8. jenkins与rebotframework搭配
  9. Android Studio一直build、一直refreshing、一直buiding gradle project into的终极解决办法
  10. freemarker中的list 前端模板