Skip to content

Simulation

This page provides a brief introduction to simulation algorithms.

Introduction

Simulation involves using a computer to simulate the operations required by a problem.

Simulation problems are typically characterized by large amounts of code, numerous operations, and complex logic. Due to the large code volume, debugging can be difficult, and making mistakes during an exam can be a significant waste of time.

Techniques

When writing simulation code, following these suggestions may improve problem-solving speed:

  • Before starting to write code, outline the process to be implemented on paper as thoroughly as possible.
  • In the code, modularize each part as much as possible by writing functions, structs, or classes.
  • For concepts that may be reused, unify and transform them for easier handling. For example, if a problem gives you "YY-MM-DD hour:minute," extract it into a function and convert it to seconds to reduce conceptual confusion.
  • Debug in blocks. The advantage of modularization is that each part can be debugged separately.
  • Write code with a clear mind. Do not write whatever comes to mind; instead, follow the steps outlined on paper.

In fact, the above steps are also helpful when solving other types of problems.

Example Explanation

Climbing Worm

A worm of negligible length is at the bottom of a well that is \(n\) inches deep. It climbs up \(u\) inches each time but must rest before climbing again. During rest, it slips down \(d\) inches. It then repeats the process of climbing and resting. How many times must the worm climb to get out of the well? If the worm reaches the top of the well exactly after climbing, we also consider it to have climbed out.

Solution

Directly simulate the worm's climbing process using a program. Use a loop to repeat the worm's climbing process, and break when the total climbing distance exceeds or equals the depth of the well.

Reference Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <iostream>

int main() {
  int n = 0, u = 0, d = 0;
  std::cin >> u >> d >> n;
  int time = 0, dist = 0;
  while (true) {  // Use an infinite loop to enumerate
    dist += u;
    time++;
    if (dist >= n) break;  // Exit the infinite loop if the condition is met
    dist -= d;
  }
  std::cout << time << '\n';  // Output the result
  return 0;
}
1
2
3
4
5
6
7
8
9
u, d, n = map(int, input().split())
time = dist = 0
while True:  # Use an infinite loop to enumerate
    dist += u
    time += 1
    if dist >= n:  # Exit the infinite loop if the condition is met
        break
    dist -= d
print(time)  # Output the obtained result
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int u = input.nextInt();
        int d = input.nextInt();
        int time = 0, dist = 0;
        while (true) {  // Use an infinite loop to enumerate
            dist += u;
            time++;
            if (dist >= n) {
                break;  // Exit the infinite loop if the condition is met
            }
            dist -= d;
        }
        System.out.println(time);   // Output the result
        input.close();
    }
}

Exercises