每天一道演算法_5_Financial Management

來源:互聯網
上載者:User

今天的題目沒法更簡單了,在考慮是不是應該有挑選性的選題目做。

題目是Financial Management,如下:

 

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.

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.

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.

100.00489.1212454.121234.10823.05109.205.271542.25839.1883.991295.011.75

$1581.42

 

import java.math.BigDecimal;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class FinancialManagement {public static void main(String args[]){Scanner in = new Scanner(System.in);List<Float> list = new ArrayList<Float>();int all = 12;while(all > 0){float f = Float.valueOf(in.nextLine());list.add(f);all --;}float sum = 0;for(int i = 0; i < list.size(); i ++){sum += list.get(i);}float average = sum/12;BigDecimal b = new BigDecimal(average);average = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();System.out.println("$"+average);}}


 

作者:jason0539

微博:

部落格:(轉載請說明出處)

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.