例如,下面给出了一组移动的例子,例子中学生的人数为8人。
0)初始队列中学生的学号依次为1, 2, 3, 4, 5, 6, 7, 8;
1)第一次调整,命令为“3号同学向后移动2”,表示3号同学出队,向后移动2名同学的距离,再插入到队列中,新队列中学生的学号依次为1, 2, 4, 5, 3, 6, 7, 8;
2)第二次调整,命令为“8号同学向前移动3”,表示8号同学出队,向前移动3名同学的距离,再插入到队列中,新队列中学生的学号依次为1, 2, 4, 5, 8, 3, 6, 7;
3)第三次调整,命令为“3号同学向前移动2”,表示3号同学出队,向前移动2名同学的距离,再插入到队列中,新队列中学生的学号依次为1, 2, 4, 3, 5, 8, 6, 7。
小明记录了所有调整的过程,请问,最终从前向后所有学生的学号依次是多少?
请特别注意,上述移动过程中所涉及的号码指的是学号,而不是在队伍中的位置。在向后移动时,移动的距离不超过对应同学后面的人数,如果向后移动的距离正好等于对应同学后面的人数则该同学会移动到队列的最后面。在向前移动时,移动的距离不超过对应同学前面的人数,如果向前移动的距离正好等于对应同学前面的人数则该同学会移动到队列的最前面。
第二行包含一个整数 m,表示调整的次数。
接下来m行,每行两个整数p, q,如果q为正,表示学号为p的同学向后移动q,如果q为负,表示学号为p的同学向前移动-q。
3
3 2
8 -3
3 -2
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
#define maxd 999999
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int student[1005];
int n,m,num,step;
void move(int num,int step){
int i,tem;
for(int i = 1;i <= n; i++)
if(student[i] == num){
tem = i;
break;
}
if(step > 0){
for(i = tem;i < tem+step;i++){
student[i] = student[i+1];
}
student[i] = num;
}
if(step < 0){
for(i = tem;i > tem+step;i--){
student[i] = student[i-1];
}
student[i] = num;
}
}
int main(int argc, char** argv){
scanf("%d",&n);
for(int i = 1;i <= n; i++)
student[i] = i;
scanf("%d",&m);
for(int i = 0;i < m; i++){
scanf("%d%d",&num,&step);
move(num,step);
}
for(int i = 1;i <= n; i++)
printf("%d ",student[i]);
return 0;
}