算法提高 交换Easy

版权声明:菜鸟一枚~~ 有想法可在下面评论, 转载标明出处即可。 https://blog.csdn.net/KLFTESPACE/article/details/82792645

问题描述

  给定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

#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
	int n, t, i, j, x, y;
	int temp;
	cin >> n >> t;
    
    int a[n+2];
    
	for (i=0; i<n; i++)
	{
		cin >> a[i];
	}
    
	while (t--)
	{
		cin >> x >> y;
		if (x == y)
			continue;
        
		temp = a[x-1];
		a[x-1] = a[y-1];
		a[y-1] = temp;
	}
	for (i=0; i<n; i++)
	{
		cout << a[i] << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/KLFTESPACE/article/details/82792645