Leetcode 2806. Account Balance After Rounded Purchase
You are given an integer representing the initial balance in a bank account. You need to implement a function that simulates a purchase. After a purchase, the balance will be reduced. However, due to some promotion, the purchase amount will be rounded to the nearest multiple of 10 before being subtracted from the account balance.
Write a function int calculateBalance(int balance, int purchaseAmount)
that takes two integer arguments: the initial balance and the purchase amount. The function should return the remaining balance after subtracting the rounded purchase amount.
#include <cmath>
int calculateBalance(int balance, int purchaseAmount) {
// Round the purchaseAmount to the nearest multiple of 10.
int roundedPurchase = round(purchaseAmount / 10.0) * 10;
// Calculate the remaining balance.
int remainingBalance = balance - roundedPurchase;
return remainingBalance;
}
round
function which rounds to the nearest integer.Feel free to ask more questions or request further clarifications!
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?