一、Description

Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems to have enough. Larry has decided that he needs to grab hold of his financial portfolio and solve his financing problems. The
first step is to figure out what's been going on with his money. Larry has his bank account statements and wants to see how much money he has. Help Larry by writing a program to take his closing balance from each of the past twelve months and calculate his
average account balance.

Input

The input will be twelve lines. Each line will contain the closing balance of his bank account for a particular 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 (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by the end-of-line. There will be
no other spaces or characters in the output.

这个问题实在太简单,要说有什么难的就是小弟的英语水平跟不上打字水平。由于实在太简单,我就不按老规矩分析了,只是指出需要注意的方面并拓展。

  • 听江湖传言,WA最多的就是精度问题了。不少同志由于不是用Java,所以精度没控制好。

对于这个问题,下面列出四种java里面的解决方法:

  1. 最简单的,System.out.println(String.format("%.2f", f));或者System.out,printf("%.2f",a);
  2. DecimalFormat df = new DecimalFormat("#.00");  System.out.println(df.format(f));
    1. NumberFormat nf = NumberFormat.getNumberInstance();
    2. nf.setMaximumFractionDigits(2);
    3. System.out.println(nf.format(f));
    1. BigDecimal bg = new BigDecimal(f);
    2. double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    3. System.out.println(f1);

感谢总结上面方法的同志!

版权声明:本文为博主原创文章,未经博主允许不得转载。

Poj_1004_FinancialManagement的更多相关文章

随机推荐

  1. Zookeeper Curator 事件监听 - 秒懂

    目录 写在前面 1.1. Curator 事件监听 1.1.1. Watcher 标准的事件处理器 1.1.2. NodeCache 节点缓存的监听 1.1.3. PathChildrenCache ...

  2. DNN 数据的表示

  3. 【python】-- 类的多继承、经典类、新式类

    继承知识点补充 在python还支持多继承,但是一般我们很少用,有些语言干脆就不支持多继承,有多继承,就会带来两个概念,经典类和新式类. 一.多继承 之前我们都是讲的单继承,那么什么是多继承呢?说白了 ...

  4. Android笔记之AsyncTask

    AsyncTask中的4个回调 onPreExecute(),在doInBackground(Params...)之前运行在UI线程中 onPostExecute(Result),在doInBackg ...

  5. 代替print输出的PY调试库:PySnooper

      PySnooper¶ Github:https://github.com/lotapp/PySnooper pip install pysnooper 使用:分析整个代码 @pysnooper.s ...

  6. MainWindows

    开发带有菜单栏状态栏等常用windows应用时候使用

  7. Java基础教程:多线程基础(1)——基础操作

    Java:多线程基础(1) 实现多线程的两种方式 1.继承Thread类 public class myThread extends Thread { /** * 继承Thread类,重写RUN方法. ...

  8. Yii2 如何更好的在页面注入CSS

    首先 先添加一个widgets,代码如下(提示:使用时注意修改命名空间) <?php /** * User: yiqing * Date: 2014/12/15 * Time: 0:21 */ ...

  9. Effective java -- 5 枚举和注解

    第三十条:用enum代替int常量enum的简单用法. enum Operation { PLUS("+") { double apply(double x, double y) ...

  10. nc命令用法

    root@10.1.1.43:~# nc -h[v1.10-38]connect to somewhere: nc [-options] hostname port[s] [ports] ... li ...