In recent years, algorithm and problem-solving skills have become important evaluation factors in the IT industry. As an extension of this trend, many companies are assessing candidates’ abilities through coding tests.
In this article, we aim to enhance basic algorithm problem-solving skills using C++ by solving the ‘Calculate ATM Withdrawal Time’ problem.
Problem Description
In the ATM, the time taken to process a withdrawal request varies for each user.
For example, if one user requests a withdrawal after 1 minute, that time is fully processed, and the user can withdraw after 1 minute.
If multiple users request withdrawals simultaneously, the requests are processed in FIFO (First-In-First-Out) order, meaning that the first requested user is served first.
Given the order of user requests and the time required for each request, write a program that calculates and outputs the total time needed for all users to complete their withdrawals.
Input Format
- The first line contains an integer N (1 ≤ N ≤ 1000) representing the number of withdrawal requests.
- The second line contains the times taken for the withdrawal requests, given as N integers separated by spaces. (1 ≤ each request time ≤ 100)
Output Format
- Output the total time taken for all users to complete their withdrawals.
Example Input
5 3 1 4 3 2
Example Output
32
Problem Solving Process
The key to solving the problem is to sum up the times needed for each user to complete their withdrawal.
In this process, we must consider the order in which the requests are processed. Since requests are handled in FIFO order, the first requested user passes first.
We follow the steps below to solve this problem.
Step 1: Read Input Values
First, we need to read the number of requests and the request times from the user. In C++, data can be read through standard input.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> times(N);
for (int i = 0; i < N; i++) {
cin >> times[i];
}
return 0;
}
Step 2: Calculate Total Time
Each user’s request time incurs a waiting time due to the requests being processed before them.
Therefore, the time taken for each user to complete their request must be the sum of their request time and the request times of previous users.
This way, from the second user onward, the waiting time will be equal to the first user’s request time.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> times(N);
for (int i = 0; i < N; i++) {
cin >> times[i];
}
int totalTime = 0;
int currentTime = 0;
for (int i = 0; i < N; i++) {
currentTime += times[i]; // Add the request time to the current time
totalTime += currentTime; // Add the current time to the total time
}
cout << totalTime << endl; // Output the final total time
return 0;
}
Step 3: Code Explanation
The first part of the code is for reading data from standard input.
vector<int> times(N);
creates a dynamic array to store the request times.
Secondly, a variable called currentTime
accumulates the time up to the current point.
Each time a user’s request comes in, the request time for that request is added to currentTime
, and this is accumulated in the total time totalTime
.
Complete Code
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N; // Input the number of requests
vector<int> times(N); // Declare a vector to hold the request times
for (int i = 0; i < N; i++) {
cin >> times[i]; // Input the time for each request
}
int totalTime = 0; // Accumulate the total request times
int currentTime = 0; // Accumulate the current processing time
for (int i = 0; i < N; i++) {
currentTime += times[i]; // Add the request time
totalTime += currentTime; // Add to the total time so far
}
cout << totalTime << endl; // Output the final total time
return 0;
}
Conclusion
By solving problems like this, we can enhance our understanding of algorithms and the C++ programming language.
It will help cultivate basic problem-solving skills that can be useful in coding tests or algorithm competitions.
It is important to build skills through various problems and prepare for interviews, and we hope this course has helped develop that foundation.