分数 100
全屏浏览题目
切换布局
作者 程序设计课题组
单位 福州大学
亚克星球上的人的素质跟我们福大人比起来,可以说是一个天上,一个地下了。他们去食堂吃饭的时候,很多人都不好好的排队,而是排在某个熟人的后面,或者直接就插到队伍的最前面。这让食堂的工作人员很是恼火。
输入格式:
第一行包含一个整数n(0<n<1000),表示有n个亚克星球上的人去食堂吃饭。
接下来n行,每行1个整数x,表示第i( i从1~n )个人排在了x的后面,当x为0时,表示第i个人排在了最前面。
输出格式:
输出仅一行,包括n个数,以空格作为分隔符,最后一个数的后面没有空格。表示这n个人最后是排成了怎样的队伍。
输入样例:
3
0
0
2
输出样例:
2 3 1
代码长度限制
16 KB
时间限制
1000 ms
内存限制
64 MB
#include<stdio.h>
#include<stdlib.h>
typedef struct student student;
struct student{
int ret;
struct student *next;
};
student *insert(int n);
void prin(student*,int n);
int main(){
int n;
student *stu1;
scanf("%d", &n);
stu1=insert(n);
prin(stu1, n);
return 0;
}
void prin(student*stu1,int n){
student *p = stu1->next;
while(p!=NULL){
if(p->next!=NULL)printf("%d ", p->ret);
else
printf("%d", p->ret);
p = p->next;
}
}
student *insert(int n){
student *head;
head = (struct student*) malloc(sizeof(student));
head->next = NULL;
student *p = head, *q;
int pos = 0;
for (int i = 1; i <= n;i++){
scanf("%d", &pos);
q = (struct student *)malloc(sizeof(student));
q->ret = i;
q->next = NULL;
if(i==1){
head->next = q;
}
else if(pos==0){
q->next = head->next;
head->next = q;
}
else if(pos!=0){
p = head->next;
while(p->ret!=pos){
p = p->next;
}
q->next = p->next;
p->next = q;
}
}
return head;
}
2480

被折叠的 条评论
为什么被折叠?



