SDUT 2109 找女朋友

本文提供了一种解决SDUT在线评测系统中编号为2109的问题的方法。通过一种特殊的排序算法实现对整数数组的排序,并输出排序后的数组元素。该算法采用双向扫描的方式,根据特定条件交换数组中的元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接  : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2109

 

 1 #include<stdio.h>  
 2 int a[100000],temp;  
 3 int main()  
 4 {  
 5     int sort(int i,int j,int *a);  
 6     int i,j,n;  
 7     scanf("%d",&n);  
 8     for(i = 0;i < n; i++)  
 9         scanf("%d",&a[i]);  
10     sort(0,n-1,a);  
11     printf("%d",a[0]);  
12     for(i = 1;i < n; i++)  
13         printf(" %d",a[i]);  
14     printf("\n");  
15     return 0;  
16 }  
17 int sort(int i,int j,int *a)  
18 {  
19     if(i == j) return 0;  
20     int h = i,t = j,mark = 0;  
21     for(;i != j;)  
22     {  
23         if(mark % 2 == 0)  
24         {  
25             if(a[i] < a[j]) {mark = 1;temp = a[i];a[i] = a[j];a[j] = temp;}  
26             else j--;  
27         }  
28         else  
29         {  
30             if(a[j] > a[i]) {mark = 0;temp = a[i];a[i] = a[j];a[j] = temp;}  
31             else i++;  
32         }  
33     }  
34     if(i == h) sort(i+1,t,a);  
35     else if(i == t) sort(h,i-1,a);  
36     else  
37     {  
38         sort(h,i-1,a);  
39         sort(i+1,t,a);  
40     }  
41 } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值