Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 阵列旋转时间(超出时间限制)_Java_Arrays - Fatal编程技术网

Java 阵列旋转时间(超出时间限制)

Java 阵列旋转时间(超出时间限制),java,arrays,Java,Arrays,我真的很困惑为什么我的java代码不起作用,它给了黑客地球上的代码僧侣们一些启示。 这是1的链接 第一个问题是和尚和轮换 import java.util.Scanner; 类TestClass{ 静态整数[]ar=新整数[100001]; 公共静态void main(字符串参数[]){ 扫描仪输入=新扫描仪(系统输入); 字节t=in.nextByte(); 而(t-->0){ int n=in.nextInt(); int k=in.nextInt()%n; 对于(int i=0;i我

我真的很困惑为什么我的java代码不起作用,它给了黑客地球上的代码僧侣们一些启示。 这是1的链接

  • 第一个问题是和尚和轮换
import java.util.Scanner;
类TestClass{
静态整数[]ar=新整数[100001];
公共静态void main(字符串参数[]){
扫描仪输入=新扫描仪(系统输入);
字节t=in.nextByte();
而(t-->0){
int n=in.nextInt();
int k=in.nextInt()%n;

对于(int i=0;i我不确定您的解决方案是否正确,但请尝试使用StreamTokenizer或BufferedReader而不是Scanner。Scanner太慢,当您需要读取大量数据时,可能会导致TLE。

导入java.util.*;
import java.util.*;
public class temp {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0){
            int n = sc.nextInt();
            int k = sc.nextInt();
            int p = 0;
            int ar[] = new int[n];
            for(int i=0;i<n;i++){
                ar[i] = sc.nextInt();
            }
            k %= n;
            for(int i=0;i<n;i++){
                p = ar[(i+(n-k))%n];
                System.out.print(p+" ");
            }
            System.out.println();
        }       
    }
}
公共类临时工{ 公共静态void main(字符串[]args){ 扫描仪sc=新的扫描仪(System.in); int t=sc.nextInt(); 而(t-->0){ int n=sc.nextInt(); int k=sc.nextInt(); int p=0; int ar[]=新的int[n];
对于(int i=0;i减少从/到System.in和System.out的读写次数。 看看下面的解决方案

        Scanner scanner = new Scanner(System.in);
        int noOfTestCases = scanner.nextInt();

        for (int i = 0; i < noOfTestCases; i++) {
            int arraySize = scanner.nextInt();
            int noOfRotations = scanner.nextInt();
            noOfRotations = noOfRotations % arraySize;
            scanner.nextLine();
            String inputString = scanner.nextLine();
            String[] inputStringArray = inputString.split(" ");

            StringBuffer sb = new StringBuffer();

            for (int j = 0; j < arraySize; j++) {
                sb.append(inputStringArray[(arraySize + j - noOfRotations) % arraySize] + " ");
            }

            System.out.print(sb);
            System.out.println("");
        }
Scanner Scanner=新的扫描仪(System.in);
int noOfTestCases=scanner.nextInt();
对于(int i=0;i
brother,我也尝试了上面的代码,甚至使用了缓冲读卡器来代替扫描仪,但仍给出了TLE。在一种情况下,网络是否也在读取输入中起作用?否。网络不应该是一个问题。如果您知道特定的测试用例,也可以脱机尝试。在任何其他语言(如CPP bec)中尝试相同的逻辑因为我以前在Java中遇到过问题,但在CPP语言中却没有。
import java.util.*;
public class temp {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0){
            int n = sc.nextInt();
            int k = sc.nextInt();
            int p = 0;
            int ar[] = new int[n];
            for(int i=0;i<n;i++){
                ar[i] = sc.nextInt();
            }
            k %= n;
            for(int i=0;i<n;i++){
                p = ar[(i+(n-k))%n];
                System.out.print(p+" ");
            }
            System.out.println();
        }       
    }
}
        Scanner scanner = new Scanner(System.in);
        int noOfTestCases = scanner.nextInt();

        for (int i = 0; i < noOfTestCases; i++) {
            int arraySize = scanner.nextInt();
            int noOfRotations = scanner.nextInt();
            noOfRotations = noOfRotations % arraySize;
            scanner.nextLine();
            String inputString = scanner.nextLine();
            String[] inputStringArray = inputString.split(" ");

            StringBuffer sb = new StringBuffer();

            for (int j = 0; j < arraySize; j++) {
                sb.append(inputStringArray[(arraySize + j - noOfRotations) % arraySize] + " ");
            }

            System.out.print(sb);
            System.out.println("");
        }