编程题#3:发票统计
注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
有一个小型的报账系统,它有如下功能:
(1)统计每个人所报发票的总钱数
(2)统计每类发票的总钱数
将此系统简化为如下:假设发票类别共有A、B、C三种;一共有三个人,ID分别为1、2、3。
输入
系统输入包含三行,每行第一个数为人员ID(整型,1或2或3),第二个数为发票总张数(张数不超过100),之后是多个发票类别(字符型,A或B或C)和相应发票金额(单进度浮点型,不超过1000.0)。
输出
输出包含六行,前三行为每人(按ID由小到大输出)所报发票总钱数(保留两位小数),后三行为每类发票的总钱数(保留两位小数)。
样例输入
1 5 A 1.0 A 2.0 C 1.0 B 1.0 C 1 3 3 B 1 C 2 C 1 2 4 B 1 A 1 C 1 A 1
样例输出
1 6.00 2 4.00 3 4.00 A 5.00 B 3.00 C 6.00
//
// main.cpp
// test
//
// Created by SpyThree on 16/9/16.
// Copyright © 2016年 SpyThree. All rights reserved.
//
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int classification_person;
char classification_type;
float number;
float amount;
float person[3] = {0, 0, 0};
float amount_A = 0;
float amount_B = 0;
float amount_C = 0;
for (int i = 0; i < 3; i ++) {
cin >> classification_person >> number;
for (int i = 0; i < number; i ++) {
cin >> classification_type >> amount;
switch (classification_type) {
case 'A':
amount_A = amount_A + amount;
break;
case 'B':
amount_B = amount_B + amount;
break;
case 'C':
amount_C = amount_C + amount;
break;
default:
break;
}
person[classification_person - 1] = person[classification_person - 1] + amount;
}
}
cout << '1' << setiosflags(ios :: fixed) << ' ' << setprecision(2) << person[0] << endl;
cout << '2' << ' ' << setprecision(2) << person[1] << endl;
cout << '3' << ' ' << setprecision(2) << person[2] << endl;
cout << 'A' << ' ' << setprecision(2) << amount_A << endl;
cout << 'B' << ' ' << setprecision(2) << amount_B << endl;
cout << 'C' << ' ' << setprecision(2) << amount_C << endl;
return 0;
}
需要注意的几个地方:
1. classification_person要设置为int
2.最后面设置输出格式的时候要注意setiosflags(ios :: fixed)可以取消无效0的省略