201703-2 学生排队 Java

思路:
将需要移动的学生remove后再add
题目中说向前向后移动不会超过人数,也就是不会出现隔着的情况。所以不会越界。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		int m = Integer.parseInt(sc.nextLine());
		List<Integer> numbers = new ArrayList<>();
		for(int i=1;i<=n;i++) {
			numbers.add(i);
		}
		for(int j=0;j<m;j++) {
			String [] line = sc.nextLine().split(" ");
			Integer sno = Integer.parseInt(line[0]);//学号
			int move = Integer.parseInt(line[1]);//移动
			int index = numbers.indexOf(sno) + move;
			numbers.remove(sno);
			numbers.add(index, sno);
		}
		sc.close();
		for(int k=0;k<n;k++) {
			System.out.print(numbers.get(k) + " ");
		}
	}

}
原文地址:https://www.cnblogs.com/yu-jiawei/p/12370445.html