试题 算法提高 交换Easy
资源限制
时间限制:1.0s 内存限制:512.0MB
问题描述
给定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
思路:
在循环中找到相应位置的数字,然后进行转换;
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int [] math = new int [n];
for(int i = 0 ;i < n; i++){
math[i] = sc.nextInt();
}
for(int i = 0;i < m;i++){
int a = sc.nextInt();
int b = sc.nextInt();
int temp = math[a-1];
math[a - 1] = math[b - 1];
math[b - 1] = temp;
}
for(int i =0;i < n;i ++){
System.out.println(math[i]);
}
}
}