leetcode2806. Round up the account balance after purchase

  • easy : https://leetcode.cn/problems/account-balance-after-rounded-purchase/

  • To start, you have $100 in your bank account.

  • You are given an integer purchaseAmount that represents the amount you are willing to spend on a purchase.

  • In a store, when you make a purchase, the actual amount spent will be rounded up to the nearest multiple of 10. In other words, you will actually pay a non-negative amount roundedAmount such that roundedAmount is a multiple of 10 and abs(roundedAmount - purchaseAmount) is the smallest value.

  • If there is more than one multiple to the nearest 10, the larger multiple is your actual payout amount.

  • Please return an integer, which represents the remaining balance after the purchase under the premise that you are willing to spend an amount of purchaseAmount.

  • Note: 0 is also a multiple of 10.

示例 1

输入:purchaseAmount = 9
输出:90
解释:这个例子中,最接近 910 的倍数是 10 。所以你的账户余额为 100 - 10 = 90
示例 2

输入:purchaseAmount = 15
输出:80
解释:这个例子中,有 2 个最接近 1510 的倍数:1020,较大的数 20 是你的实际开销。
所以你的账户余额为 100 - 20 = 80
 

提示:

0 <= purchaseAmount <= 100

code

class Solution {
    
    
public:
    int accountBalanceAfterPurchase(int purchaseAmount) {
    
    
        int roundedAmount =0;
        if (purchaseAmount % 10 >= 5){
    
    
            roundedAmount = 10;
        }

        return 100 - (purchaseAmount/10)*10 - roundedAmount ;
    }
};

Guess you like

Origin blog.csdn.net/ResumeProject/article/details/132164523