← 返回 openai 的题目列表GPU Credits II (Modified): Balance Query Returns None When Subtract Fails
类型:online_judge
Problem: GPU Credits II (Modified)
Implement a simple GPU credit account system that supports adding credits, subtracting credits, and querying balances.
Required API
Design a class/module that supports at least:
add(user, amount): add amount credits to user.
subtract(user, amount): subtract amount credits from user.
If the current balance is insufficient, the subtraction must not take effect.
getBalance(user): return the current balance for user.
Modification: when the “current credits are not enough to subtract” (i.e., the most recent subtraction attempt for that user failed due to insufficient balance), getBalance(user) must return None. Otherwise return the integer balance.
No need to optimize for time complexity; a clear correct implementation is sufficient.
I/O (one possible judge format)
Read commands from stdin and print the result of each GET on its own line:
ADD user amount
SUB user amount
GET user
Assume:
user is a string without spaces
amount is a non-negative integer
Constraints
Number of commands N: 1 to 2e5
amount: 0 to 1e9
Sample Tests
input:
ADD alice 10
GET alice
output:
10
input:
ADD alice 10
SUB alice 3
GET alice
output:
7
input:
ADD alice 5
SUB alice 10
GET alice
output:
None
input:
ADD bob 0
GET bob
SUB bob 1
GET bob
output:
0
None
input:
ADD alice 10
SUB alice 6
SUB alice 5
GET alice
ADD alice 1
GET alice
output:
None
5
Example
Input
ADD alice 10
GET alice
Output
10