Michael graduated of ITESO this year and finally has a job. He’s making a lot of money, but somehownever seems to have enough. Michael has decided that he needs to grab hold of his financial portfolioand 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 Michaelby writing a program to take his closing balance from each of the past twelve months and calculate hisaverage account balance.
Input
The first line of input contains a single integer N, (1 ≤ N ≤ 100) which is the number of datasets thatfollow.
Each dataset consists of twelve lines. Each line will contain the closing balance of his bank accountfor a particular month. Each number will be positive and displayed to the penny. No dollar sign willbe included.
Output
For each dataset, you should generate one line of output with the following values: The dataset numberas 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
问题链接:UVA11945 Financial Management
题意简述:输入12个数,计算其平均值。
问题分析:(略)
程序说明:
编写程序时,考虑到C++处理输入比较方便,所以使用C++语言编程。然而,输出稍微麻烦一些,对于金额,每3位需要加一个逗号,而C和C++的函数库中,没有相应的解决办法。
程序中专门编写函数output_result()处理输出,使用的是有限状态自动机的工作方式来处理的。
这个问题与参考链接的问题基本上相同,只是输入输出数据格式略有不同。
参考链接:UVALive2362 POJ1004 HDU1064 ZOJ1048 Financial Management
AC的C语言程序如下:
/* UVA11945 Financial Management */ #include <iostream> #include <cstdio> #include <cstring> using namespace std; void output_result(char s[], char t[]) { int len = strlen(s); int i = len - 1, j=0; int state = 0, count; while(i >=0) { count++; t[j++] = s[i]; if(s[i] == '.') { state = 1; count = 0; } else if(s[i] == '$') state = 2; if(state == 1 && count == 3 && s[i-1] != '$') t[j++] = ','; i--; } j--; while(j >= 0) putchar(t[j--]); } int main() { int t2; double val, sum; char s[128], t[128]; cin >> t2; for(int i=1; i<=t2; i++) { sum = 0; for(int j=1; j<=12; j++) { cin >> val; sum += val; } sprintf(s, "%d $%.2f\n", i, sum / 12); output_result(s, t); } return 0; }