2017-06-03 20 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]) ST MK手段 - N> + - >結果%nを(あなたにリマインダーを与えます) - - >%n =結果。最後に[結果]が与えられます。結果が有効なインデックスである場合、結果が配列のサイズより大きい場合は、配列から有効な結果が得られますArrayIndexOutOfBoundsException

関連する問題