#K41462. Account Balance Statement Generator
Account Balance Statement Generator
Account Balance Statement Generator
You are provided a sequence of financial transactions for various accounts. Each transaction is represented by an account ID, a transaction type (deposit
or withdraw
), and an amount. Your task is to compute the final balance for each account after processing all the transactions.
The input begins with an integer n, indicating the number of transactions. This is followed by n lines, each containing a transaction in the format:
account_id transaction_type amount
The output is a list of accounts and their final balances, sorted in lexicographical order by account ID. Each line should contain an account ID and its corresponding balance separated by a space.
The balance is computed as follows:
- If the transaction type is
deposit
, the amount is added to the account balance. - If the transaction type is
withdraw
, the amount is subtracted from the account balance.
In mathematical terms, if B
is the balance for account i
, then for each transaction with amount a
and type t
:
$$
B_i = B_i + \begin{cases} a & \text{if } t = \text{deposit} \\ -a & \text{if } t = \text{withdraw} \end{cases}
$$
inputFormat
The first line of the input contains an integer n representing the number of transactions.
This is followed by n lines, each containing three elements separated by spaces:
- account_id (a string)
- transaction_type (a string: either
deposit
orwithdraw
) - amount (an integer)
You should read the input from standard input (stdin).
outputFormat
Output the final balance for each account on separate lines. Each line should contain the account ID, followed by a space, and then its balance.
The accounts must be printed in lexicographical order (alphabetical order by account ID).
The output should be written to standard output (stdout).
## sample5
A123 deposit 100
B456 withdraw 50
A123 withdraw 40
A123 deposit 300
B456 deposit 200
A123 360
B456 150
</p>