老师给了小乐乐一个正整数序列,要求小乐乐把这个序列去重后按从小到大排序。但是老师给出的序列太长了,小乐乐没办法耐心的去重并排序,请你帮助他。
老师给了小乐乐一个正整数序列,要求小乐乐把这个序列去重后按从小到大排序。但是老师给出的序列太长了,小乐乐没办法耐心的去重并排序,请你帮助他。
第一行包含一个正整数n,表示老师给出的序列有n个数。接下来有n行,每行一个正整数k,为序列中每一个元素的值。(1 ≤ n ≤ 105,1 ≤ k ≤ n)
输出一行,为去重排序后的序列,每个数后面有一个空格。
4 2 2 1 1
1 2
5 5 4 3 2 1
1 2 3 4 5
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; set<int> s; for(int i = 0; i < n; i++){ int num; cin >> num; s.insert(num); } for(auto i : s) cout << i << " "; cout << endl; return 0; }
import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner input=new Scanner(System.in); int n=input.nextInt(); HashSet<Integer> set=new HashSet<Integer>(); for(int i=0;i<n;i++) { set.add(input.nextInt()); } List<Integer> list=new ArrayList<Integer>(set); Collections.sort(list); for(Integer l:list) System.out.print(l+" "); } }
定义一个集合,输入元素后,集合过滤去重,将过滤后的元素放到list中,再利用Collections.sort(list);
输出。一个小的注意事项:HashSet的迭代器在输出时“不保证有序”,但也不是“保证无序”。也就是说,输出时有序也是允许的,但不保证实现的时候每次输出都是有序。
#include<iostream> #include<algorithm> using namespace std; int main() { int n; cin>>n; int* a=new int[n]; for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n); int m=unique(a,a+n)-a; for (int i = 0; i <m; i++) cout << a[i] << " "; delete []a; return 0; }
while(line = readline()){ var n = parseInt(line); var res =[]; for(var i=0;i<n;i++){ var nline = parseInt(readline()); res.push(nline); } res = [...new Set(res)].sort(function(a,b){ return a-b }) console.log(res.join(" ")); }
#include <iostream> #include <algorithm> using namespace std; int main() { int n,k=0; cin>>n; int a[n],b[n];//修改为静态数组a[10001]出错,不清楚是怎么回事,哈哈 for(int i=0;i<n;++i) { cin>>a[i]; b[i]=0; } for(int i=0;i<n;++i) { if(b[a[i]]==0) { a[k++]=a[i]; b[a[i]]=1; } } sort(a,a+k); for(int i=0;i<k;++i) cout<<a[i]<<" "; return 0; }
// Compiled in C++14(g++5.4) #include <bits/stdc++.h> using namespace std; namespace IO { template <typename T> inline void read(T& t) { int n = 0; int c = getchar_unlocked(); t = 0; while (!isdigit(c)) n |= c == '-', c = getchar_unlocked(); while (isdigit(c)) t = t * 10 + c - 48, c = getchar_unlocked(); if (n) t = -t; } template <typename T, typename... Args> inline void read(T& t, Args&... args) { read(t); read(args...); } template <typename T> inline void write(T x) { if (x < 0) x = -x, putchar_unlocked('-'); if (x > 9) write(x / 10); putchar_unlocked(x % 10 + 48); } template <typename T> inline void writeln(T x) { write(x); putchar_unlocked('\n'); } } int main() { int n; IO::read(n); vector<int> A(n); for (auto &i : A) { IO::read(i); } sort(A.begin(), A.end()); auto uniq = unique(A.begin(), A.end()); for (auto it = A.begin(); it != uniq; ++it) { IO::write(*it); putchar_unlocked(' '); } putchar_unlocked('\n'); return 0; }
// Compiled in C++14(g++5.4) #include <bits/stdc++.h> using namespace std; constexpr int MAXN = 100005; namespace IO { template <typename T> inline void read(T& t) { int n = 0; int c = getchar_unlocked(); t = 0; while (!isdigit(c)) n |= c == '-', c = getchar_unlocked(); while (isdigit(c)) t = t * 10 + c - 48, c = getchar_unlocked(); if (n) t = -t; } template <typename T, typename... Args> inline void read(T& t, Args&... args) { read(t); read(args...); } template <typename T> inline void write(T x) { if (x < 0) x = -x, putchar_unlocked('-'); if (x > 9) write(x / 10); putchar_unlocked(x % 10 + 48); } template <typename T> inline void writeln(T x) { write(x); putchar_unlocked('\n'); } } bitset<MAXN> A; int main() { int n, val; int max_val = -1; int min_val = MAXN; IO::read(n); for (int i = 0; i < n; ++i) { IO::read(val); A[val] = true; max_val = max(max_val, val); min_val = min(min_val, val); } for (int i = min_val; i <= max_val; ++i) { if (A[i]) { IO::write(i); putchar_unlocked(' '); } } putchar_unlocked('\n'); return 0; }
#include <stdio.h> int main(){ int n,i,input,max = 0; int a[100001]={0}; scanf("%d",&n); for(int i = 0; i< n; i++){ scanf("%d",&input); a[input] = input; // 把相同的数存进相同的数组元素中, if(input >= max){ // 这样该数组元素只会保留一个相同的数 max = input; } } for(i=1;i<=max;i++){ if(a[i]){ //按序输出非零元素 printf("%d ",a[i]); } } return 0; }在网上参考的,这个解法很巧妙,没有用排序算法和去重,而是利用了数组自然增序的特点进行去重排序。
#include<stdio.h> #include<malloc.h> int cmp(const void* e1, const void* e2) { return *(int*)e1 - *(int*)e2; } int main() { int n,i,j; scanf("%d",&n); int* arr = (int*)malloc(n * sizeof(int)); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(arr[j]==arr[i])//相同则置零 arr[j]=0; } } qsort(arr,n,sizeof(int),cmp);//数据排序 for(i=0;i<n;i++) { if(arr[i]!=0) { printf("%d ",arr[i]); } } return 0; }
#include<stdio.h> int main() { int i, n, k; while (~scanf("%d", &n)) { int arr[100001] = { 0 }; int max = 0; //出现过的数字,该元素改为1,作为标记 for (i = 0; i < n; i++) { scanf("%d", &k); arr[k] = 1; if (max < k) max = k; } //正序输出标记过的数 for (i = 0; i <= max; i++) { if (arr[i]) printf("%d ", i); } printf("\n"); } return 0; }
#include<stdio.h> int main() { int arr[100000]= {0}; int n = 0; scanf("%d",&n); int i = 0; int k = 0; //每一行的正整数 for(i = 0;i<n;i++) { scanf("%d",&k); arr[k] = k; } for(i = 0;i<n;i++) //打印 { if(arr[i])//不为0的时候打印 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; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> int main() { int n = 0; scanf("%d", &n); int i = 0,ar[100]={0},re=0; for (i = 0; i < n; i++) { scanf("%d", &ar[i]); } int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i > j && ar[i] == ar[j]) { ar[i] = -1; } } } for (i = 0; i < n-1; i++) { for (j = 0; j < n - 1 - i; j++) { if (ar[j - 1] > ar[j]) { re = ar[j-1]; ar[j - 1] = ar[j]; ar[j] = re; } } } for (i = 0; i < n; i++) { if (ar[i] > 0) { printf("%d ", ar[i]); } } return 0; }
n=int(input()) number=[] for i in range(0,n): num=int(input()) if num not in number: number.append(num) number.sort() for i in range(0,len(number)): print(number[i],end=" ")