2017-06-03 37 views
-6

有人可以解释这个公式正在做什么,请。这段代码是什么意思?实施

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

下面是如果您感到困惑的问题链接:https://www.hackerrank.com/challenges/circular-array-rotation –

回答

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 

     } 
    } 
} 

一[((MK)%N + N)%N])装置1 ST MK - >结果%N(给你提示) - > + N - - >%n =结果。在末尾一个[结果]将被给予。如果结果是一个有效的索引,那么你将从数组得到有效的结果,如果不是这意味着如果结果超过你将得到的数组的大小ArrayIndexOutOfBoundsException