代码实现
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
int n;
cin >> n;
int a[1000] = { 0 };
for (int j = 0; j < n; j++)
{
cin >> a[j];
}
int i, data;
cin >> i;
cin >> data;
if (i >n)
{
cout << "error!";
}
else if (i == n)
{
a[n] = data;
for (int j = 0; j < n; j++)
{
cout << a[j] << " ";
}
cout << a[n] << "\n";
}
else
{
n++;
for (int j = n; j >i-1;j--)
{
a[j] = a[j - 1];
}
a[i-1] = data;
for (int j = 0; j < n-1; j++)
{
cout << a[j]<<" ";
}
cout << a[n-1]<<"\n";
}
return 0;
}