1 条题解
-
0
解题思路
解析输入数据:
首先解析银行对账单和登记记录的数据。
存储对账单信息:
将对账单中的交易存储在一个字典中,键为交易类型和编号,值为金额。
处理登记记录:
遍历登记记录中的每笔交易,检查其是否在对账单中,金额是否正确,是否重复,以及计算是否正确。
生成输出:
根据检查结果生成相应的输出,并在最后输出遗漏的交易。
解题方法
读取对账单:
读取起始余额和最终余额。
读取所有交易并存储到字典中,键为 (type, ),值为 。
读取登记记录:
读取起始余额。
读取交易和后续余额,检查每笔交易的正确性。
检查交易:
检查交易是否在对账单中。
检查交易是否重复。
检查金额是否正确。
检查计算是否正确。
输出遗漏交易:遍历对账单中的所有交易,检查是否在登记记录中出现过,未出现的按顺序输出。
C++代码实现:
#include <iostream> #include <vector> #include <map> #include <set> #include <string> #include <algorithm> #include <iomanip> #include <sstream> #include <cmath> using namespace std; struct Transaction { string type; int number; double amount; }; struct TransactionComparator { bool operator()(const pair<string, int>& a, const pair<string, int>& b) const { if (a.first == b.first) { return a.second < b.second; } return a.first < b.first; } }; void processInput() { string line; double startBalance, endBalance; map<pair<string, int>, double, TransactionComparator> statementTransactions; set<pair<string, int>, TransactionComparator> registerTransactions; set<pair<string, int>, TransactionComparator> missingTransactions; vector<pair<Transaction, double> > registerEntries; // Read bank statement cin >> line >> startBalance; while (cin >> line && line != "balance") { string type = line; int number; double amount; cin >> number >> amount; pair<string, int> key = make_pair(type, number); statementTransactions[key] = amount; } cin >> endBalance; // Read register entries double currentRegisterBalance; cin >> currentRegisterBalance; while (cin >> line) { string type = line; int number; double amount; cin >> number >> amount; Transaction t; t.type = type; t.number = number; t.amount = amount; double newBalance; cin >> newBalance; registerEntries.push_back(make_pair(t, newBalance)); } // Process each register entry for (size_t i = 0; i < registerEntries.size(); ++i) { const pair<Transaction, double>& entry = registerEntries[i]; const Transaction& t = entry.first; double registeredAmount = t.amount; double registeredNewBalance = entry.second; double expectedNewBalance; pair<string, int> key = make_pair(t.type, t.number); bool inStatement = statementTransactions.find(key) != statementTransactions.end(); bool repeated = registerTransactions.find(key) != registerTransactions.end(); bool correctAmount = false; if (inStatement) { correctAmount = fabs(statementTransactions[key] - registeredAmount) < 1e-9; } bool mathMistake = false; bool mathUsesCorrectValue = false; vector<string> errors; if (!inStatement) { errors.push_back("is not in statement"); } if (repeated) { errors.push_back("repeated transaction"); } if (inStatement && !correctAmount) { errors.push_back("incorrect amount"); } // Calculate expected new balance with registered amount if (i == 0) { expectedNewBalance = startBalance; } else { expectedNewBalance = registerEntries[i-1].second; } if (t.type == "check") { expectedNewBalance -= registeredAmount; } else if (t.type == "deposit") { expectedNewBalance += registeredAmount; } if (fabs(expectedNewBalance - registeredNewBalance) >= 1e-9) { mathMistake = true; if (inStatement && !correctAmount) { // Calculate expected new balance with correct amount double expectedNewBalanceWithCorrectAmount; if (i == 0) { expectedNewBalanceWithCorrectAmount = startBalance; } else { expectedNewBalanceWithCorrectAmount = registerEntries[i-1].second; } if (t.type == "check") { expectedNewBalanceWithCorrectAmount -= statementTransactions[key]; } else { expectedNewBalanceWithCorrectAmount += statementTransactions[key]; } if (fabs(expectedNewBalanceWithCorrectAmount - registeredNewBalance) < 1e-9) { mathUsesCorrectValue = true; } } } if (mathUsesCorrectValue) { errors.push_back("math uses correct value"); } else if (mathMistake) { errors.push_back("math mistake"); } // Output the result for this transaction cout << t.type << " " << t.number; if (errors.empty()) { cout << " is correct" << endl; } else { for (size_t j = 0; j < errors.size(); ++j) { if (j == 0) { cout << " "; } else { cout << " "; } cout << errors[j]; } cout << endl; } // Mark as processed registerTransactions.insert(key); } // Collect missing transactions for (map<pair<string, int>, double, TransactionComparator>::const_iterator it = statementTransactions.begin(); it != statementTransactions.end(); ++it) { if (registerTransactions.find(it->first) == registerTransactions.end()) { missingTransactions.insert(it->first); } } // Output missing transactions in order vector<pair<string, int> > sortedMissing(missingTransactions.begin(), missingTransactions.end()); sort(sortedMissing.begin(), sortedMissing.end(), TransactionComparator()); for (vector<pair<string, int> >::const_iterator it = sortedMissing.begin(); it != sortedMissing.end(); ++it) { cout << "missed " << it->first << " " << it->second << endl; } } int main() { processInput(); return 0; }
- 1
信息
- ID
- 1078
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 4
- 已通过
- 1
- 上传者