试题编号 | 201703-2 |
试题名称 | 学生排队 |
时间限制 | 1.0s |
内存限制 | 256.0MB |
问题描述 |
问题描述 体育老师小明要将自己班上的学生按顺序排队。他首先让学生按学号从小到大的顺序排成一排,学号小的排在前面,然后进行多次调整。一次调整小明可能让一位同学出队,向前或者向后移动一段距离后再插入队列。 输入格式 输入的第一行包含一个整数n,表示学生的数量,学生的学号由1到n编号。 输出格式 输出一行,包含n个整数,相邻两个整数之间由一个空格分隔,表示最终从前向后所有学生的学号。 样例输入 8 样例输出 1 2 4 3 5 8 6 7 评测用例规模与约定 对于所有评测用例,1 ≤ n ≤ 1000,1 ≤ m ≤ 1000,所有移动均合法。 |
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
int id;
struct student *next;
}Student,*ListStudent;
void InitialList(ListStudent &L,int n);
void MoveList(ListStudent &L,int number,int pos);
int main()
{
int i,n,m,p,q;
ListStudent L,temp;
scanf("%d",&n);
InitialList(L,n);
scanf("%d",&m);
for(i = 0; i < m; i ++)
{
scanf("%d %d",&p,&q);
MoveList(L,p,q);
}
temp = L;
for( i = 0; i < n; i ++)
{
temp = temp->next;
if(i != n-1)
{
printf("%d ",temp->id);
}
else{
printf("%d\n",temp->id);
}
}
return 0;
}
void InitialList(ListStudent &L,int n)
{
int i;
ListStudent s,t;
L = (ListStudent)malloc(sizeof(Student));
L->next = NULL;
t = L;
for( i = 0; i < n; i ++)
{
s = (ListStudent)malloc(sizeof(Student));
s->id = i+1;
t->next = s;
t = s;
}
t->next = NULL;
}
void MoveList(ListStudent &L,int number,int pos)
{
int cnt = 0;
ListStudent s,final,move;
final = L;
s = L;
while( s->next->id != number)
{
if(pos < 0 && ( pos+cnt ) >= 0)
{
final = final->next;
}
s = s->next;
cnt ++;
}
if(pos>0)
{
final = s;
}
while(pos >= 0 )
{
final = final->next;
pos--;
}
move = s->next;
s->next = s->next->next;
move->next = final->next;
final->next = move;
}