问题描述
思路
找到需要调整位置的学号后,若往前调,则将该学号后面的所有学号依次后移,反之前移
完整代码
#include<iostream>
using namespace std;
const int N = 1010;
int queen[N];
int n, m, num, pos;
int main()
{
cin >> n >> m;
for( int i = 0; i < n; i++)
{
queen[i] = i + 1;
}
int temp; //暂存在队列中找到的学号所在位置
for( int i = 0; i < m; i++)
{
cin >> num >> pos;
for( int j = 0; j < n; j++)
{//找到所指学号
if( num == queen[j])
{
temp = j;
break;
}
}
if( pos > 0)
{//向后移位
for( int j = temp; j < temp + pos; j++)
{
queen[j] = queen[j+1];
}
queen[temp+pos] = num;
}
else if( pos < 0)
{//向前移位
for( int j = temp; j > temp + pos; j--)
{
queen[j] = queen[j-1];
}
queen[temp+pos] = num;
}
}
for( int i = 0; i < n; i++)
cout << queen[i] << ' ';
return 0;
}