No es posible pensar en una manera de cambiar matriz

BipoN:

Estoy atascado y no puedo pensar en una manera de cambiar adecuadamente una matriz por __ unidades. Estoy tratando de crear una matriz de 30 elementos (números 1-30) que puede ser desplazado hacia la derecha por el número de las entradas del usuario. Esto significaría que los primeros números de la matriz tomarían el índice de al final de la matriz, y el resto de los números se desplazaría hacia la izquierda. (Ex, si de cambio = 3, números 1,2,3 tomaría el índice de 27,28,29, y el resto de los números 4-30 se desplazaría a la izquierda haciendo índice 0 = 4, índice 1 = 5, índice 2 = 6 ....

import java.util.*;

class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner (System.in);

    System.out.println("\nEnter the shift/rotation:");
    int shiftNum = input.nextInt();

    int [] numArray = new int [30];

    for(int i = 0; i < 30; i++){
        numArray [i] = i+1;
        System.out.print(numArray[i]+" ");
    }

  }
}

Este es el código que tengo hasta ahora, alguna sugerencia de cómo puedo hacer esto? He tratado de hacer un aparte para bucle como

numArray [i-shiftNum] = numArray[i];

Pero al hacer esto, el índice de 0-shiftNum sería negativo y no funcionaría. Este es el contexto del problema:

Crear un programa que va a crear una matriz de 30 elementos. Entonces se girará la matriz por un número seleccionado por el usuario.

vimal:

Aquí es una solución rápida para usted. Por favor verifique siguiente código.

entrada:

Introduzca el desplazamiento / rotación: 4

salida:

Girar dado array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]

Después de girar [27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 , 21, 22, 23, 24, 25, 26]

    public static void main(String[] args) {
    RotationDemo rd = new RotationDemo();
    int[] input = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
    int k = 0;
    Scanner scan = new Scanner (System.in);
    try{
         System.out.println("\nEnter the shift/rotation:");
         int shiftNum = scan.nextInt();
         if(shiftNum < 30) {
             k = shiftNum;
             System.out.println("Rotate given array " + Arrays.toString(input));
             int[] rotatedArray = rd.rotateRight(input, input.length, k);
             System.out.println("After Rotate  " + 
                  Arrays.toString(rotatedArray));
         } else {
            System.out.println("Shift number should be less than 30");
         }
         } catch(Exception ex){
         } finally {
            scan.close();
        }
      }
      public int[] rotateRight(int[] input, int length, int numOfRotations) {
        for (int i = 0; i < numOfRotations; i++) {
          int temp = input[length - 1];
          for (int j = length - 1; j > 0; j--) {
            input[j] = input[j - 1];
          }
          input[0] = temp;
        }
        return input;
      }

Esperamos que este ejemplo funciona.

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=330270&siteId=1
Recomendado
Clasificación