排序问题
排序问题Time Limit: 1000MS Memory limit: 65536K
题目描述
输入10个整数,将它们从小到大排序后输出,并给出现在每个元素在原来序列中的位置。
输入
输入数据有一行,包含10个整数,用空格分开。
输出
输出数据有两行,第一行为排序后的序列,第二行为排序后各个元素在原来序列中的位置。
示例输入
1 2 3 5 4 6 8 9 10 7
示例输出
1 2 3 4 5 6 7 8 9 10 1 2 3 5 4 6 10 7 8 9
这虽然是个很简单的问题,但是练一练链表的插排还是不错的。。。。。。。
#include <stdio.h> #include <stdlib.h> struct linshi { int num; int no; struct linshi *next; }; struct linshi *head = NULL; struct linshi *ls = NULL; struct linshi *p = NULL; struct linshi *q = NULL; int main() { int n=10,i=0; head=(struct linshi *)malloc(sizeof(struct linshi)*1); head->next=NULL; p=(struct linshi *)malloc(sizeof(struct linshi)*1); p->next=NULL; q=(struct linshi *)malloc(sizeof(struct linshi)*1); q->next=NULL; i=0; while(n--) { i++; ls=(struct linshi *)malloc(sizeof(struct linshi)*1); ls->next=NULL; scanf("%d",&ls->num); ls->no=i; if(n==9) { head->next=ls; ls->next=NULL; } else { q=head; p=head->next; while(p!=NULL) { if(ls->num<p->num) { q->next=ls; ls->next=p; break; } q=p; p=p->next; } if(p==NULL) { q->next=ls; q=ls; q->next=NULL; } } } p=head->next; while(p!=NULL) { printf("%d",p->num); if(p->next!=NULL) printf(" "); p=p->next; } printf("\n"); p=head->next; while(p!=NULL) { printf("%d",p->no); if(p->next!=NULL) printf(" "); p=p->next; } return 0; }

1万+

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



