这段代码是什么意思?履行

时间:2017-06-03 18:46:12

标签: java implementation

请有人解释这个公式正在做什么。

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]);
        }
    }
}

1 个答案:

答案 0 :(得分: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

        }
    }
}

a [((m-k)%n + n)%n])表示1 st m-k - >结果%n(给你提醒) - &gt; + n - &gt;%n =结果。最后将给出 a [result] 。如果结果是一个有效的索引,那么你将从数组得到有效的结果,如果不是这意味着如果结果超过你将得到的数组的大小 ArrayIndexOutOfBoundsException