24h購物| | PChome| 登入
2013-08-11 13:53:27| 人氣2,069| 回應0 | 上一篇 | 下一篇

[UVA] 11945 - Financial Management

推薦 0 收藏 0 轉貼0 訂閱站台


  D: Financial Management 

Michael graduated of ITESO this year and finally has a job. He's making a lot of money, but somehow never seems to have enough. Michael 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.

Michael has his bank account statements and wants to see how much money he has. Help Michael by writing a program to take his closing balance from each of the past twelve months and calculate his average account balance.

Input 

The first line of input contains a single integer N, ( 1$ le$N$ le$100) which is the number of datasets that follow.

Each dataset consists of 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 

For each dataset, you should generate one line of output with the following values: The dataset number as a decimal integer (start counting at one), a space, a character `$', and a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny.


Note: Used comma (,) to separate the thousands.

Sample Input 

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

Sample Output 

1 $1,581.42

計算 12 個月的平均四捨五入
格式化輸出,用內建要看編譯器,這裡是手動。

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
void dollar(long long n) {
    if(n < 100) {
        printf("0.%02lld", n);
        return;
    }
    char buf[20];
    sprintf(buf, "%lld", n);
    int len = strlen(buf);
    int i;
    for(i = 0; i < len-2; i++) {
        putchar(buf[i]);
        if(i == len-6)
            putchar(',');
    }
    printf(".%s", buf+len-2);
}
int main() {
    int testcase, cases = 0;
    int i;
    long long a, b;
    scanf("%d", &testcase);
    while(testcase--) {
        long long sum = 0;
        for(i = 0; i < 12; i++) {
            scanf("%lld.%lld", &a, &b);
            sum += 100*a+b;
        }
        int avg = sum/12 + (sum%12 >= 6);
        printf("%d $", ++cases);
        dollar(avg);
        puts("");
    }
    return 0;
}
/*
5
100.00
100.00
100.00
100.00
100.00
100.00
100.00
100.00
100.00
100.00
100.00
100.00
*/


台長: Morris
人氣(2,069) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA] 139 - Telephone Tangles
此分類上一篇:[UVA] 857 - Quantiser

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文