题目描述
建立一个顺序表L,然后以第一个为分界,将所有小于等于它的元素移到该元素的前面,将所有大于它的元素移到该元素的后面。
输入
顺序表长度n; 顺序表中的数据元素。输出
移动后的数据元素。样例输入
10 32 5 22 43 23 56 54 57 11 25样例输出
25 11 23 22 5 32 43 56 54 57#include<iostream> #include<malloc.h> #include<cstring> using namespace std; #define INF 0x3f3f3f3f #define M 1000 typedef struct { int data[M]; int length; }sqlist; void initlist(sqlist*&L) { L=(sqlist*)malloc(sizeof(sqlist)); L->length=0; } void createlist(sqlist*&L,int n) { for(int i=0;i<n;i++) { cin>>L->data[i]; } L->length=n; } void sortlist(sqlist*&L1,sqlist*&L2) { int i; int num=L1->data[0]; int idx=0; for(i=L1->length-1;i>=0;i--) { if(L1->data[i]<num) { L2->data[idx]=L1->data[i]; idx++; } } for(i=0;i<L1->length;i++) { if(L1->data[i]>=num) { L2->data[idx]=L1->data[i]; idx++; } } L2->length=L1->length; } void outlist(sqlist*L2) { for(int i=0;i<L2->length;i++) { cout<<L2->data[i]<<" "; } } int main() { sqlist*L1,*L2; initlist(L1); initlist(L2); int n; cin>>n; createlist(L1,n); sortlist(L1,L2); outlist(L2); return 0; }
SWUST OJ1102: 顺序表上数据的划分问题的实现
最新推荐文章于 2023-01-13 10:41:51 发布