#include<stdio.h>
int setPartition(int a[],int n){
int pviot,low=0,low0=0,high=n-1,high0=n-1,flag=1,k=n/2,i;
int s1=0,s2=0;
while(flag){
pviot=a[low];
while(low<high){
while(low<high && a[high]>=pviot) --high;
if(low!=high) a[low]=a[high];
while(low<high && a[low]<=pviot) ++low;
if(low!=high) a[high]=a[low];
}
a[low]=pviot;
if(low==k-1){
flag=0;
}else{
if(low<k-1){
low0=++low;
high=high0;
}else{
high0=--high;
low=low0;
}
}
}
//while(a[k]==a[k-1]){//集合中不可能有相同元素存在啊;
// k=k+1;//若把集合换为数组,当出现相同元素时,为保证不相交的条件,应把相同元素放到一个集合中
//}
for(i=0;i<k;i++){
s1+=a[i];
printf("%d ",a[i]); //4 2 1 5
}
printf("\n");
for(i=k;i<n;i++){
s2+=a[i];
printf("%d ",a[i]); //7 6 8 9
}
printf("\n");
return s2-s1;
}
int main(){
int a[]={8,9,1,7,4,6,2,5,3};
int len=sizeof(a)/sizeof(int);
printf("%d ",setPartition(a,len-1));//结果为18
return 0;
}
2016年408考研真题43题
猜你喜欢
转载自blog.csdn.net/Aaron_Kings/article/details/102652215
今日推荐
周排行