题解 | Simple Sorting
Simple Sorting
https://www.nowcoder.com/practice/139761e0b59a405786898b7f2db9423f
#include <stdio.h> #define dataLen 1000 int merge(int data[dataLen], int start, int mid, int end){ int result[dataLen]; int k=0; int i=start; int j=mid+1; // 排序 while(i<=mid && j<=end){ if(data[i]<data[j]){ result[k++] = data[i++]; }else{ result[k++] = data[j++]; } } // 处理剩余的 while(i<=mid){ result[k++] = data[i++]; } while(j<=end){ result[k++] = data[j++]; } // 复制 for(i=start,j=0; j<k; i++,j++){ data[i] = result[j]; } return 0; } int mergeSort(int data[dataLen], int start, int end){ if(start>=end) return 0; int mid = (start+end)/2; mergeSort(data, start, mid); mergeSort(data, mid+1, end); merge(data, start, mid, end); return 0; } int main() { int n; int data[dataLen]; while(scanf("%d", &n) != EOF){ for(int i=0; i<n; i++){ scanf("%d", &data[i]); } // 排序 mergeSort(data, 0, n-1); // 去重+输出 int k=0; while(k<n){ if(data[k]==data[k+1]){ k++; }else{ printf("%d ", data[k]); k++; } } } return 0; }