分数 100
全屏浏览题目
切换布局
作者 程序设计课题组
单位 福州大学
晨跑小组决定从今天开始晨跑了,为了使晨跑的队伍看上去比较整齐,于是小组成员决定按身高从高到低排成一列。现在已知晨跑小组每个成员的身高。小组成员们都想知道自己排在第几位,前面和后面相邻的人分别是谁。请编写程序帮助他们实现吧。
输入格式:
输入第一行包括一个正整数N(1<=N<=1000),表示晨跑小组的人数。
接下来一行有N个浮点数Ai,Ai表示第i个人的身高(1<=i<=N)。(数据中没有两个人身高相同的情况)。
输出格式:
输出N行,每行有三个数据Ri,Fi,Bi。
Ri表示第i个人排在第几位。
Fi表示第 i个人前面相邻人的编号,如果前面没有人,输出0。
Bi表示第i个人后面相邻人的编号,如果后面没有人,输出0。
输入样例:
5
178.5
177.4
165.4
164.6
173.3
输出样例:
1 0 2
2 1 5
4 5 4
5 3 0
3 2 3
代码长度限制
16 KB
时间限制
1000 ms
内存限制
64 MB
#include<stdio.h>
#include<stdlib.h>
typedef struct student student;
//还是双链表好用
struct student{
student *prior;
student *next;
int ri, bi, fi,ret;
double height;
student *arret;
};
student* create(int n){
student *head, *p, *q;
head = (student*)malloc(sizeof(student));
head->prior = NULL;
head->next = NULL;
head->arret = NULL;
for (int i = 1; i <= n;i++){
q = (student*)malloc(sizeof(student));
scanf("%lf", &q->height);
q->ret = i;
if(i==1){
head->next = q;
head->arret = q;
q->arret = NULL;
q->prior = head;
q->next = NULL;
}else {
p->next = q;
p->arret = q;
q->arret = NULL;
q->prior = p;
q->next = NULL;
}
p = q;
}
return head;
}
void swap(student *p1,student *p2){
student *p1f, *p1b, *p2f, *p2b;
p1f = p1->prior;
p1b = p1->next;
p2f = p2->prior;
p2b = p2->next;
p1f->next = p1b;
p1b->prior = p1f;
p1b->next = p2f;
p2f->prior = p1b;
p2f->next = p2b;
if(p2b!=NULL)
p2b->prior = p2f;
}
int main(){
int n;
scanf("%d", &n);
student *stu1;
stu1=create(n);
student *p = stu1->next;
for (int i = 1; i < n;i++){
p = stu1->next;
for (int j = 1; j < n - i + 1;j++){
if(p->height<p->next->height){
swap(p,p->next);
}
else p = p->next;
}
}
p = stu1->next;
for (int i = 1; i <= n;i++){
if(i==1){
p->fi = 0;
p->bi = p->next->ret;
}else if(i==n){
p->bi = 0;
p->fi = p->prior->ret;
}else {
p->fi = p->prior->ret;
p->bi = p->next->ret;
}
p->ri = i;
p = p->next;
}
p = stu1->arret;
for (int i = 1; i <= n;i++){
printf("%d %d %d\n", p->ri, p->fi, p->bi);
p = p->arret;
}
return 0;
}