为每个测试分配不同的名称以存储用户输入并访问它

  • 本文关键字:用户 存储 访问 测试 分配 java
  • 更新时间 :
  • 英文 :


第一行由一个整数T组成,表示测试用例的数量。

对于每个测试用例:

  1. 第一行由两个整数N和K组成,N是数组中元素的数量,K表示旋转的步数
  2. 下一行由N个空格分隔的整数组成,表示数组A的元素

样本输入

1
5 2
1 2 3 4 5

样本输出

4 5 1 2 3

我的代码

import java.util.Scanner;
import java.util.ArrayList;
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner scan = new Scanner(System.in);

int test=scan.nextInt();
scan.nextLine();
int size;
int steps;
int temp=0;
for(int j=1;j<=test;j++){
ArrayList<Integer> array+j = new ArrayList<Integer>();
size = scan.nextInt();
steps = scan.nextInt();
scan.nextLine();
for(int i=0;i<size;i++){
array+j.add(scan.nextInt());
}


for(int i=0;i<steps;i++){

temp = array+j.get(size-1);
array+j.remove(size-1);
array+j.add(0,temp);

}
if(j==test){
for(j=1;j<=test;j++){
for(int i=0;i<size;i++){
System.out.print(array+j.get(i)+" ");
}
}
}
}


}
}

这个程序有错误,因为我们不能分配不同的名称,如

for(int j=1;j<=test;j++){
ArrayList<Integer> array+j = new ArrayList<Integer>();
}

那么,我该如何实现

这是你的问题的答案吗?

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt(); // read test cases
while (T-- > 0) {
int N = scanner.nextInt(); // read number of integers
int K = scanner.nextInt(); // read number of permutations
int[] array = new int[N];
for (int i = 0; i < N; i++) {
array[i] = scanner.nextInt(); // store numbers in array
}
for (int i = 0; i < N; i++) {
System.out.print(array[(N - K + i) % N] + " "); // print element N - K as the first element
}
System.out.println(); // return to new line after each test case
}
scanner.close();
}
}

最新更新