Using the idea of circular queue of encryption and decryption (Los Valley P1914 problem solution to a problem)

Foreword

In my opinion, help us understand the circular queue thinking is:
rational use "%" of the constraints, the issue of "cross-border" bounce back within the scope of the problem.

This article is an example of thinking circular queue provides a way for me to quickly spike this question.

Questions asked

P1914 topic Link

Here Insert Picture Description

analysis

We want to achieve 'z' is 'a', so that the value is equivalent to the right to return within the bounds of the left margin.
This is like a common effect: a piercing line from the right side of the screen, and from the left side of the penetration.

And in my opinion this idea is much like a circular queue.
On one side of a circular queue "filled" and then "spill false", will fill the new element on the other side.

Of course, there will be a queue is full of problems, but this is a good idea.

Like saying "pinball game" implementation problem is bound to rebound after the completely elastic collision with a way to simulate conservation of momentum, not forced out of bounds; but here the solution is allowed to come back from the other side of bounds.

Realization of this idea, is to rely on "%" operator to achieve.

However, this question is a bit tricky: 'a' is not char = 0, so we should let the current char-'a ', then + offset, and finally 26% (because there are 26 letters of the alphabet, this is the range boundary), then + 'a', we are asking can be obtained.

This is the title of this so-called encryption and decryption slightly.

AC Code (Java description language)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt(scanner.nextLine());
        char[] password_array = scanner.nextLine().toCharArray();
        for (int i = 0; i < password_array.length; i++) {
            password_array[i] = (char)((password_array[i] - 'a' + num) % 26 + 'a');
        }
        System.out.println(new String(password_array));
        scanner.close();
    }
}

Published 350 original articles · won praise 609 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_43896318/article/details/104059930