/*
A
20 40 32 67 40 20 89 300 400 15
*/
#include <stdio.h>
/*自己写的这个quicksort错了。(while那部分)
void quicksort(int arr[],int first,int last)
{
int First=first,Last=last;
int dest=arr[0],tempt=0;
while(last>first){
while(last>first&&arr[last]<dest){
arr[tempt]=arr[last];
last--;
}
while(last>first&&arr[first]>dest){
arr[last]=arr[first];
first++;
}
arr[first]=dest;
}
quicksort(arr,First,first-1);
quicksort(arr,last+1,Last);
}*/
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivot = arr[low];
int i = low;
int j = high;
while (i < j) {
while (i < j && arr[j] >= pivot)
j--;
arr[i] = arr[j];
while (i < j && arr[i] <= pivot)
i++;
arr[j] = arr[i];
}
arr[i] = pivot;
quickSort(arr, low, i - 1);
quickSort(arr, i + 1, high);
}
}
int quchong(int arr[],int len){
int count=0;
for(int m=0;m<len-1;m++){
for(int n=m+1;n<len;n++){
if(arr[m]==arr[n]){
for(int k=n;k<len;k++){
arr[k]=arr[k+1];
}
len--;
n--;
count++;
}
}
}
return count;
}
另一:这个用的是冒泡排序【方便将A和D放一起操作】,以及在去重这里,不一样的思路。
int main()
{
char ch;
scanf("%c",&ch);
int num[100]={0};
int i=0;
while((scanf("%d",&num[i]))!=EOF) i++;
printf("\n");
quickSort(num,0,i-1);
//quchong(num,i);
int Count=quchong(num,i);
if(ch=='A')
for(int j=0;j<i-Count;j++) printf("%d ",num[j]);
if(ch=='D')
for(int j=i-1-Count;j>=0;j--) printf("%d ",num[j]);
//printf("\n");
}