For each case, the first line of the input contains an integer number N representing the quantity of numbers in this array(1≤N≤1000). Next N lines contain N integer numbers(one number per each line) of the original array.
For each case ,outtput file should contain at most N numbers sorted in ascending order. Every number in the output file should occur only once.
6 8 8 7 3 7 7
3 7 8
#include<stdio.h> int main() { int n,a[1000],i,j,temp; scanf("%d",&n);//输入 for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++)//冒泡 for(j=0;j<n-1-i;j++) if(a[j]>a[j+1]) {//交换 temp=a[j];a[j]=a[j+1];a[j+1]=temp; } for(i=0;i<n-1;i++)//去重 {//判断从0-n-2个数,最后一个数n-1不判断是因为后面没有能与他相同的了 if(a[i]==a[i+1])//本位置与下一个位置的数相等则顺序前移 { for(j=i;j<n;j++)//从本位置顺序前移 a[j]=a[j+1]; i--;//还从当前位置判断 n--;//去掉一个数数组容量减一 } } //输出 for(i=0;i<n;i++) printf("%d ",a[i]); }
#include<iostream> #include<algorithm> using namespace std; int main(){ int n; while(cin>>n){ int* a=new int[n]; for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n); for(int i=0;i<n;i++){ if((a[i]!=a[i-1]&&i>0&&i<n-1)||i==0) cout<<a[i]<<" "; if(i==n-1&&a[i]!=a[i-1]) cout<<a[i]; } cout<<endl; } }
__author__ = 'Yaicky' import copy while True: try: n = input() numList = map(int, raw_input().strip().split()) numList.sort() rlt = copy.deepcopy(numList) last = 9999999 for i in numList: if i == last: rlt.remove(i) else: last = i rlt = map(str, rlt) print ' '.join(rlt) except: break
#include <stdio.h> (737)#include <stdlib.h> #include <algorithm> //这个C语言没有的 using namespace std; int main() { int n; while(~scanf("%d", &n)) { int array[1000]; for(int i=0; i<n; i++) { scanf("%d", &array[i]); } sort(array, array+n); //C语言不给用sort的话可自定义冒泡排序函数 printf("%d ", array[0]); //先输入排序好的第一个数 for(int i=1; i<n; i++) { if(array[i] != array[i-1]) //去重操作 printf("%d ", array[i]); } } }各位python大神别秀了,这些高校复试题怎么可能给用python呢,C++给不给用都是看心情的🤣
#include <iostream> #include<vector> #include<algorithm> using namespace std; int main() { //这里选择先排序,再将遍历后的元素遍历一遍来去重 int n; cin >> n; vector<int>tmp(n); for (int i = 0; i < n; i++) { int x; cin >> x; tmp[i] = x; } sort(tmp.begin(), tmp.end()); int res[n]; res[0] = tmp[0]; int j = 1; for (int k = 1; k < n; k++) { if (tmp[k] == tmp[k - 1])continue; res[j++] = tmp[k]; } for (int s = 0; s < j; s++) { cout << res[s]; cout << " "; } cout << endl; }
#include "bits/stdc++.h" using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while (!(c <= '9' && c >= '0')) { if (c == '-') { f = -1; } c = getchar(); } while (c <= '9' && c >= '0') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return x * f; } set<int>s; int main() { int n=read(); for(int i=1;i<=n;i++){ s.insert(read()); } for(auto t:s){ cout<<t<<" "; } return 0; }
'''归并排序实现''' def merge(a, b): i = 0 j = 0 c = [] while i < len(a) and j < len(b): if a[i] < b[j]: c.append(a[i]) i+=1 elif a[i] > b[j]: c.append(b[j]) j+=1 c.extend(a[i:]) c.extend(b[j:]) return c def merge_sort(a, l, r): if r - l <= 1: return a[l:r] m = (l + r) // 2 left = merge_sort(a, l, m) right = merge_sort(a, m, r) res = merge(left, right) return res def sim(a): return merge_sort(a, 0, len(a)) while True: try: a = [] n = int(input()) e = list(map(int, input().split())) e = set(e) #print(set(e)) e = list(e) #print(e) e = sim(e) e = map(str, e) e = ' '.join(e) print(e) except: break
#include <iostream> #include <set> using namespace std; int main() { int n; while (cin >> n) { //运用C++内置泛型模板类set存储数据,set内部用红黑树实现, //元素自动有序,且自动排除重复元素 set<int>mySet; while (n--) { int number; cin >> number; mySet.insert(number); } for (const auto& e : mySet) { //顺序输出set中的元素 cout << e << " "; } cout << endl; } return 0; }
#include <cstdio> #include <iostream> #include <set> using namespace std; int main(){ int n; while(scanf("%d",&n) != EOF){ int a; set<int> num; for(int i = 0; i < n; ++i){ scanf("%d",&a); num.insert(a); } set<int>::iterator it; for(it = num.begin();it != num.end();++it){ cout<<*it<<" "; } printf("\n"); } return 0; }
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.stream.Stream; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { int n = sc.nextInt(); List<Integer> list = new ArrayList<>(); for(int i=0;i<n;++i) { list.add(sc.nextInt()); } Stream stream = list.stream(); stream.distinct().sorted().forEach(i->System.out.print(i+" ")); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int n = in.nextInt();//数据个数 int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } Arrays.sort(a);//对数组a排序 ArrayList<Integer> b = new ArrayList(); for (int i = 0; i < n; i++) { if (!b.contains(a[i]))//保证不出现重复的数据 b.add(a[i]); } for (Integer i : b) System.out.print(i + " "); } } }