题目描述
给定N个整数组成的序列,每次交换当前第x个与第y个整数,要求输出最终的序列。
输入
第一行为序列的大小N(1< =N< =1000)和操作个数M(1< =M< =1000)。
第二行包含N个数字,表示初始序列。
接下来M行,每行两个整数x,y (1< =x,y< =N),表示要交换的两个整数。在一次交换中,如果x和y相等,则不会改变序列的内容。
输出
输出N行,为交换后的序列中的数。
样例输入
5 2 1 2 3 4 5 1 2 3 4
样例输出
2 1 4 3 5
package parctice;
import java.util.Scanner;
public class ph1491 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
int [] arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < M; i++) {
int A = sc.nextInt()-1;
int B = sc.nextInt()-1;
// 交换
int temp=arr[A];
arr[A]=arr[B];
arr[B]=temp;
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}