Java algorithm interstellar communication

Posted by keithschm on Tue, 14 Dec 2021 11:00:12 +0100

Title Description

Man finally landed on the land of Mars and met the mysterious Martians. Neither humans nor Martians can understand each other's language, but our scientists have invented a way to communicate digitally. This communication method is like this. First, the Martians tell human scientists a very large number. After scientists solve the meaning of this number, they add a small number to the large number and tell the results to the Martians as human answers.
Martians use a very simple way to represent numbers - breaking fingers. Martians have only one hand, but there are thousands of fingers on this hand. These fingers are in a row, numbered 1, 2, 3. Any two fingers of Martians can exchange positions at will. That's how they count.
A Martian demonstrated how to count with his fingers with a human hand. If you number the five fingers - thumb, index finger, middle finger, ring finger and little finger as 1, 2, 3, 4 and 5 respectively, when they are arranged in the normal order, they form 5 digits 12345. When you exchange the positions of ring finger and little finger, they will form 5 digits 12354. When you completely reverse the order of the five fingers, they will form 54321, which is among all the 120 5 digits that can be formed, 12345 minimum, which represents 1; 12354 second small, which represents 2; 54321 is the largest, which means 120. The following table shows the six three digits that can be formed when there are only three fingers and the numbers they represent:
Ternary number
  123
  132
  213
  231
  312
  321
Number represented by
  1
  2
  3
  4
  5
  6
Now you are lucky to be the first person on earth to communicate with Martians. A Martian will show you his fingers, and scientists will tell you a small number to add. Your task is to add the number represented by Martian fingers to the number told by scientists, and change the order of Martian fingers according to the addition result. Input data to ensure that the result will not exceed the range that Martian fingers can represent.

input
Enter Description:
It includes three lines. The first line has a positive integer n, which represents the number of Martian fingers (1 < = n < = 10000). The second line is a positive integer M, which represents the small integer to be added (1 < = M < = 100). The next line is an arrangement of N integers from 1 to N, separated by spaces, showing the arrangement order of Martian fingers.
Input example:
5
3
1 2 3 4 5

output

Output Description:
There is only one line, which contains N integers, indicating the changed order of Martian fingers. Every two adjacent numbers shall be separated by a space, and there shall be no redundant space
Output example:
1 2 4 5 3

HINT: time limit: 1.0s memory limit: 256.0MB
For 30% data, n < = 15;
For 60% of the data, n < = 50;
For all data, n < = 10000;

Problem solving ideas

The main problem is sorting. You can first judge whether the size of the last two bits after the exchange position is larger or smaller than that before the exchange. If it is larger after the exchange, record it once. If it is smaller than that, you need to shift the last bit to the digit 1 smaller than it, and move the rest backward in turn. Also note that there should be no spaces at the end of the output

code

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static boolean nextPermutation(int[] array) {
        if(array.length<=1)//No next full permutation
        {
            return false;
        }
        int i = array.length-2;//Find the first number that does not meet the descending order from the back to the front (taking into account the repeated numbers)
        for(;i>=0&&array[i]>array[i+1];i--)
        {

        }
        if(i==-1)        //No full permutation
        {
            return false;
        }
        int k =i+1;        //Starting from I, find the smallest number greater than arr[i]
        for(;k<array.length&&array[k]>array[i];k++)
        {

        }
        swap(array,i,k-1);        //Exchange arr[i] and arr[k-1]
        Arrays.sort(array,i+1,array.length);        //Reorder the numbers after arr[i], and then continue the full arrangement operation
        return true;
    }
    public static void swap(int [] array,int i,int j){
        int t =array[i];
        array[i] = array[j];
        array[j] =t;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int[] s = new int[n];
        for (int i = 0; i < n; i++) {
            s[i] = scanner.nextInt();//Fill in data
        }
        int count = 0;
        do {

            if (count == m) {
                for (int j : s) {
                    System.out.print(j + " ");
                }
                break;
            }
            count++;

        } while (nextPermutation(s));
        scanner.close();
    }
}

Topics: Java Algorithm