#include<iostream> #include<algorithm> using namespace std; int main() { int i; cin>>i; int* a=new int[i]; for(int j=0;j<i;j++) cin>>a[j]; sort(a,a+i); int n=unique(a,a+i)-a; for(int k=0;k<n;k++) cout<<a[k]<<" "; delete []a; return 0; }
利用Set集合不允许重复元素的特点,来去掉重复元素
import java.util.*; public class Main { public static void main(String[] args){ Scanner s =new Scanner(System.in); int a= s.nextInt(); HashSet set = new HashSet(); for(int i=1;i<=a;i++){ set.add(s.nextInt()); } List list = new ArrayList(set); //升序排序 Collections.sort(list); for (Integer integer : list) { System.out.print(integer+" "); } }
import java.util.Scanner; public class Main { public static void main(String args[]){ int n,b,t=0; int a[]=new int[1001]; Scanner s=new Scanner(System.in); n=s.nextInt(); while((--n)>=0) { b=s.nextInt(); a[b]=1; } s.close(); while(t<1001) { if(a[t]==1) System.out.print(t+" "); t++; } } }
int main() { int n = 0; scanf("%d",&n); int arr[50] = { 0 }; int i = 0; for (i=0;i<n;i++) { scanf("%d",&arr[i]); } int tmp = 0; int k = 0; int g = 0; int sum = 0; for (i = 0; i < n; i++) { int j = 0; for (j=i+1;j<n-sum;j++) { if (arr[i]==arr[j]) { int g = j; for (k=j;k<n-1;k++) { tmp = arr[g]; arr[g] = arr[g + 1]; arr[g + 1] = tmp; g++; } sum++; } } } for (i = 0; i < n; i++) { int j = 0; for (j=i+1;j<n-sum;j++) { if (arr[i]==arr[j]) { int g = j; for (k=j;k<n-1;k++) { tmp = arr[g]; arr[g] = arr[g + 1]; arr[g + 1] = tmp; g++; } sum++; } } } for (i = 0; i < n-sum; i++) { int j = 0; int tmp = 0; for (j=0;j<n-sum-1;j++) { if (arr[j] > arr[j + 1]) { tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } for (i = 0; i < n-sum; i++) { printf("%d ",arr[i]); } return 0; }
#include <stdio.h> #include <malloc.h> #include <assert.h> void QuickSort(int* left, int* right) { assert(left && right); //只剩1个元素,无需继续排序 //开始回归 if (left >= right) { return; } int* i = left; int* j = right; int pivot = *left; while (i < j) { //j在小于pivot的地方停下来 while (*j >= pivot && j > i) { j--; } *i = *j; //i在大于pivot的地方停下来 while (*i <= pivot && i < j) { i++; } *j = *i; } //将基准元素pivot放入它该在的位置 *j = pivot; QuickSort(left, i - 1);//i是上一轮已经处理好的元素,所以 -1 处理左边的元素 QuickSort(j + 1, right);//j是上一轮已经处理好的元素,所以 +1 处理右边的元素 } int main() { int n = 0; int i = 0; int* p = NULL; scanf("%d", &n); //开辟空间 p = (int*)malloc(sizeof(int) * n); if (!p) { perror("malloc:p"); return 1; } //录入数列 for (i = 0; i < n; i++) { scanf("%d", p + i); } //进行排序 QuickSort(p, p + n - 1); //进行去重输出 printf("%d ", *p); for (i = 1; i < n; i++) { if (*(p + i) == *(p + i - 1)) { continue; } printf("%d ", *(p + i)); } //释放空间 free(p); p = NULL; return 0; }
#include<stdio.h> int main() { int n = 0; scanf("%d", &n); int arr[1001] = { 0 }; int m = 0; for (int i = 0; i < n; i++) { scanf("%d", &m); arr[m] = m; } for (int i = 0; i <= 1000; i++) { if (arr[i] != 0) { printf("%d ", arr[i]); } } return 0; }
#include <stdio.h> #include <stdlib.h> int main() { int a, b, tmp; int n, size = 0; int* arr = NULL; //申请n个字节动态空间 while (arr == NULL) { scanf("%d", &n); arr = (int*)malloc(n * sizeof(int)); } //输入数据,重复的不进行储存 for (a = 0; a < n; a++) { scanf("%d", arr + size); size++; for (int i = 0; i < size-1; i++) { if (*(arr + size-1) == *(arr + i)) { size--; break; } } } //进行排序 for (a = 0; a < size-1; a++) { for (b = 0; b < size - a - 1; b++) { if (*(arr + b) > *(arr + b + 1)) { tmp = *(arr + b); *(arr + b) = *(arr + b + 1); *(arr + b + 1) = tmp; } } } for (a = 0; a < size; a++) { printf("%d ", *(arr + a)); } return 0; }