#include <bits/stdc++.h>
using namespace std;
int x[10000];
int main()
{
	int n;
	cin >> n;
	
	for (int i = 0; i<n ; i++)
	{
		cin>>  x[i];
	}
	
	int temp;
	cin >> temp;
	while (temp>0)
	{
		int old, nwe;
		
		cin >> old >> nwe;
		
		n++;
		int na = x[old+1];
		for (int j=old; j<n; j++ )
		{
			int nb = x[j+1];
			x[j+1]=na;
			na = nb;
			
			
		}
		
		x[n+1] = na;
		x[old+1]=nwe;
		
		temp--;
	}
	int tempp=0;
	while (n>0)
	{
		
		cout << x[tempp] << " ";
		tempp++;
		n--;
	}
	return 0;
	
}

1 comments

  • @ 2025-12-20 11:57:12

    在当前数组的 axa_xax+1a_{x+1} 之间插入 yy

    读题啊!你看看你中间往后交换的数是怎么换的,是要从插入位置依次往后移动。

    while (temp>0)
    	{
    		int old, nwe;
    		
    		cin >> old >> nwe;
    		
    		n++;
    		int na = x[old+1];
    		for (int j=old; j<n; j++ )
    		{
    			int nb = x[j+1];
    			x[j+1]=na;
    			na = nb;
    			
    			
    		}
    		
    		x[n+1] = na;
    		x[old+1]=nwe;
    		
    		temp--;
    	}
    

    正常就从后往前顺次移动就好了,简单的赋值运算 a[i + 1] = a[i];,你交换会影响后面的数值。

    #include <bits/stdc++.h>
    using namespace std;
    int n, m, x, y;
    int a[21234];
    int main()
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        cin >> m;
        for (int t = 1; t <= m; t++)
        {
            cin >> x >> y;
            for (int i = n; i >= x + 1; i--)
                a[i + 1] = a[i];
            a[x + 1] = y;
            n++;
        }
        for (int i = 1; i <= n; i++)
            cout << a[i] << " ";
        return 0;
    }
    • 1

    Information

    ID
    588
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    # Submissions
    368
    Accepted
    130
    Uploaded By