2017-06-03 2 views
-6

Quelqu'un peut-il expliquer ce que cette formule est en train de faire, s'il vous plaît.Que signifie ce code? Mise en œuvre

import java.io.*; 
import java.util.*; 
import java.text.*; 
import java.math.*; 
import java.util.regex.*; 

public class Solution{ 
    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 
     int n = in.nextInt(); 
     int k = in.nextInt(); 
     int q = in.nextInt(); 
     int[] a = new int[n]; 
     for(int a_i=0; a_i < n; a_i++){ 
      a[a_i] = in.nextInt(); 
     } 
     for(int a0 = 0; a0 < q; a0++){ 
      int m = in.nextInt(); 
      System.out.println(a[((m-k)%n+n)%n]); 
     } 
    } 
} 
+0

Voici le lien vers la question si vous êtes confus: https://www.hackerrank.com/challenges/circular-array-rotation –

Répondre

0
import java.io.*;//importing all the classes in java.io package and same with all below import statements. 
import java.util.*; 
import java.text.*; 
import java.math.*; 
import java.util.regex.*; 

public class Solution{ //creating a class named Solution 
    public static void main(String[] args) { //main methid where the execution of the program begins. 
     Scanner in = new Scanner(System.in);//scanner is class by using which you can take the input from the user through the console. so you are creating an object of "Scanner" class here. 
     int n = in.nextInt();//this will take the next int type from the user through the console 
     int k = in.nextInt();//this will take the next int type from the user through the console 
     int q = in.nextInt();//this will take the next int type from the user through the console 
     int[] a = new int[n];//creating an array of size n.this means you can store n number of values in the array.rememeber n is the number given by user.if user says 5 i.e you can store 5 values in the array. 
     for(int a_i=0; a_i < n; a_i++){//itrating over the n value 
      a[a_i] = in.nextInt();//taking the integer value from the user and pusing in to the 'a_i' index 
     } 
     for(int a0 = 0; a0 < q; a0++){//iterating till value q 
      int m = in.nextInt();//taking the m value from the use 
      System.out.println(a[((m-k)%n+n)%n]);//performing mathematical operation 

     } 
    } 
} 

un [((mk)% n + n)% n]) signifie 1 er mk -> Résultat% n (vous donne le rappel) -> + n - ->% n = résultat. à la fin un [résultat] sera donné. Si le résultat est un index valide, vous obtiendrez un résultat valide du tableau si ce n'est pas le cas si le résultat est supérieur à la taille du tableau que vous obtiendrez ArrayIndexOutOfBoundsException.