Кotlin Coding Test Course, Calculating ATM Withdrawal Time

Written on: October 17, 2023

Author: Algorithm Expert

Table of Contents

  1. 1. Problem Description
  2. 2. Input Format
  3. 3. Output Format
  4. 4. Example
  5. 5. Approach
  6. 6. Code Implementation
  7. 7. Conclusion

1. Problem Description

You are a user of an ATM. There are several users waiting at the ATM, and each user is in line to withdraw money from their account.

The time it takes for each user to withdraw money can vary. The goal of this problem is to calculate the total time it takes for all users to complete their withdrawals.

The input provided to you is a list of withdrawal times for each user. All users will use the ATM in order, and the next user can only use it after the user in front has finished.

The total withdrawal time is the sum of the time each user spends waiting in front of the ATM and the time taken to complete the withdrawal. You will write a program in Kotlin to calculate this.

2. Input Format

The first line contains the number of users N (1 <= N <= 1,000).

The second line contains the withdrawal times for each user, separated by spaces. The withdrawal time is at least 1 second and at most 1,000 seconds.

3. Output Format

Print the total withdrawal time for all users in one line.

4. Example

Example Input

5
3 1 4 3 2
        

Example Output

32
        

Description

When the withdrawal times for each user are given, calculate the waiting time and withdrawal time in order.

User 1: 3 seconds

User 2: 3 seconds + 1 second = 4 seconds

User 3: 4 seconds + 4 seconds = 8 seconds

User 4: 8 seconds + 3 seconds = 11 seconds

User 5: 11 seconds + 2 seconds = 13 seconds

Total time = 3 + 4 + 8 + 11 + 13 = 32 seconds

5. Approach

To solve this problem, you can approach it by sequentially accumulating the withdrawal times to calculate the total time.
The specific approach to solving the problem is as follows:

  1. Input the number of users N and the withdrawal times as a list.
  2. Sort this list. (It can be processed differently according to the problem’s requirements)
  3. Calculate the total withdrawal time by accumulating each user’s withdrawal time.
  4. Finally, output the total time.

6. Code Implementation

Below is the Kotlin code for solving the problem.

fun main() {
    val n = readLine()!!.toInt()
    val times = readLine()!!.split(" ").map { it.toInt() }.sorted()
    var totalTime = 0
    var accumulateTime = 0
    
    for (time in times) {
        accumulateTime += time
        totalTime += accumulateTime
    }
    
    println(totalTime)
}
        

Code Explanation

– First, read the number of users N, then read the withdrawal times as a list on the next line.
– After sorting the input times, calculate the total time by accumulating each user’s withdrawal time.
– Finally, print the result.

7. Conclusion

In this lecture, we covered the problem of calculating ATM withdrawal times. It is essential in algorithm problem-solving to understand the problem accurately and to clarify the approach.

Additionally, it is crucial to implement the code through languages like Kotlin, as it allows you to gain practical experience alongside theoretical knowledge.
Such practice will help you achieve high scores in coding tests.

I hope you continue to hone your algorithmic thinking by solving various problems.