输入包括两行: 第一行为序列长度n(1 ≤ n ≤ 50) 第二行为n个数sequence[i](1 ≤ sequence[i] ≤ 1000),以空格分隔
输出消除重复元素之后的序列,以空格分隔,行末无空格
9 100 100 100 99 99 99 100 100 100
99 100
#include<iostream> using namespace std; int main () { int n; cin>>n; int seq[50]; for(int i=0;i<n;i++) { cin>>seq[i]; } for(int i=n-1;i>=0;i--) { int b=seq[i]; for(int j=i-1;j>=0;j--) { if(b-seq[j]==0) { seq[j]=0; } } } for(int i=0;i<n;i++) { if(seq[i]!=0) { cout<<seq[i]; if(i<(n-1)) cout<<' '; } } }
package go.jacob.day913; import java.util.ArrayList; import java.util.Scanner; public class Demo3 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } ArrayList<Integer> res=new ArrayList<Integer>(); for(int i=arr.length-1;i>=0;i--){ if(!res.contains(arr[i])) res.add(arr[i]); } System.out.print(res.get(res.size()-1)); for(int i=res.size()-2;i>=0;i--){ System.out.print(" "+res.get(i)); } sc.close(); } }
#include<iostream>
using namespace std;
int all_count=0;
int flag=1;
void compare(int c[50],int d[50],int count,int num)
{
if(count==all_count)
{
for(int i=(flag-1);i>=0;i--)
{
cout<<d[i];
if(i>0)
cout<<" ";
}
cout<<endl;
return;
}
for(int i=(num-1);i>=0;i--)
{
int p=0;
for(int j=0;j<flag;j++)
{
if(c[i]==d[j])
p=1;
}
if(p==0)
d[flag++]=c[i];
}
count++;
compare(c,d,count,num);
}
int main(void)
{
int n;
cin>>n;
all_count=n;
int a[50],b[50];
for(int i=0;i<n;i++)
{
int o;
cin>>o;
a[i]=o;
}
b[0]=a[n-1];
compare(a,b,1,n);
}
import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * @Author qgfzzzzzz * @Date 2019/8/19 * @Version 1.0 * <p> * * </p> */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] arr = new int[n]; Map<Integer, Integer> map = new HashMap<>(); for(int i = 0; i < arr.length; i++){ arr[i] = scanner.nextInt(); map.put(arr[i], map.getOrDefault(arr[i], 0) + 1); } StringBuilder stringBuilder = new StringBuilder(); for(int i = 0; i < arr.length; i++){ if(map.get(arr[i]) == 1) stringBuilder.append(arr[i]).append(" "); else{ int val = map.get(arr[i]); map.put(arr[i], val - 1); } } System.out.println(stringBuilder.toString().trim()); } }
#include<iostream> #include<vector> #include<set> using namespace std; int main() { //数据输入 int n; cin >> n; vector<int> sequence; for (int i = 0; i < n; i++) { int t; cin >> t; sequence.push_back(t); } //数据处理,利用set的值唯一特性 set<int> temp; vector<int>result; for (int i = sequence.size()-1; i >=0; i--) if (temp.find(sequence[i])==temp.end()) { temp.insert(sequence[i]); result.push_back(sequence[i]); } cout << result[result.size() - 1]; for (int i = result.size() - 2; i >= 0; i--) cout << " " << result[i]; return 0; }
import java.util.ArrayDeque; import java.util.Deque; import java.util.Scanner; /** *使用一个双端队列即可,比较简单,直接show the code 关键代码就五行 Deque<Integer> q = new ArrayDeque<>(); for (int i = n - 1; i >= 0; i --) { if (!q.contains(sequence[i])) { q.offerFirst(sequence[i]); } } */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] sequence = new int[n]; for (int i = 0; i < n; i ++) { sequence[i] = sc.nextInt(); } Deque<Integer> q = new ArrayDeque<>(); for (int i = n - 1; i >= 0; i --) { if (!q.contains(sequence[i])) { q.offerFirst(sequence[i]); } } int count = q.size(); for (int i = 0; i < count - 1; i ++) { System.out.print(q.poll() + " "); } System.out.println(q.poll()); } }
# -*- coding: UTF-8 -*- #倒着读取,已有则不添加,再倒着打印即可。 import sys n = int(raw_input().strip()) value=map(int,sys.stdin.readline().strip().split()) value.reverse() i=0 arr=[] while i<n: if value.index(value[i]) == i: arr.append(value[i]) i=i+1 arr.reverse() for x in arr: print x,
import java.util.*; /** * 此题巧妙地用到了List去重的方法。 * 比如说有一组数,为1 5 5 1 6 1 * 从[0,n-1]使用List去重之后,保存地其实就是每种元素最开始出现的那个元素。 * 但是我们换个角度去想,如果我们从后向前遍历,保存地就是每种元素最后出现的那个元素,只不过相对从前到后来说,数倒过来了。 * 我们最终将数反转过来即可。 * 时间复杂度为O(n) * @author 何嘉龙 * */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()) { int n = in.nextInt(); int[] arr = new int[n]; List<Integer> lists = new ArrayList<>(); for(int i = 0; i < n; i++) { arr[i] = in.nextInt(); } for(int i = n - 1; i >= 0; --i) { if(!lists.contains(arr[i])) { lists.add(arr[i]); } } for(int i = lists.size() - 1; i >= 0; --i) { System.out.print(lists.get(i)); if(i != 0) { System.out.print(" "); } } } in.close(); } }
import java.util.*;
/*
* 此题需要考虑从后往前遍历,可以使用map结构,针对不存在的情况才进行添加
* */
public class RemoveDuplicateLast {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
int[] nums = new int[n];
Map<Integer, Integer> map = new HashMap<>();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
}
int index = 0;
for (int i = n - 1; i >= 0; i--) {
// 不出现的情况才添加进来
if (!map.containsKey(nums[i])) {
map.put(nums[i], 1);
list.add(nums[i]);
index++;
}
}
System.out.print(list.get(index - 1));
for (int i = index - 2; i >= 0; i--) {
System.out.print(" " + list.get(i));
}
System.out.println();
}
}
}
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] input = new int[n]; for(int i = 0; i < n; i++) input[i] = sc.nextInt(); int curP = n; Set<Integer> set = new HashSet<>(); for(int i = n-1; i >= 0; i--) { if(set.add(input[i])) input[--curP] = input[i]; } System.out.print(input[curP++]); for(; curP < n; ) System.out.print(" " + input[curP++]); System.out.println(); } }
//对于array[i]使用倒叙list.contains()方法去重 //再反向输出list import java.nio.charset.MalformedInputException; import java.security.KeyStore.Entry; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.Vector; import javax.xml.transform.Templates; public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub /* 9 100 100 100 99 99 99 100 100 100 */ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int [] array = new int [n]; List<Integer> list = new ArrayList<>(); for(int i = 0;i < n;i++){ array[i] = scanner.nextInt(); } for(int i = n-1;i >= 0;i--){ if(list.contains(array[i]) == false){ list.add(array[i]); } } System.out.print(list.get(list.size()-1)); for(int i = list.size() - 2;i >= 0;i--){ System.out.print(" " + list.get(i)); } } }
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
List<String> list=new ArrayList<>();
for (int i = 0; i < n; i++) {
String num=sc.next();
if(list.contains(num)) list.remove(num);
list.add(num);
}
for (int i=0;i<list.size();i++) {
if(i!=list.size()-1){
System.out.print(list.get(i)+" ");
}else{
System.out.println(list.get(i));
}
}
}
}
#include<stdlib.h> #include<stdio.h> #include<string.h> #include<unordered_map> using namespace std; void reomveOverLapStrs(const int* array, int n){ unordered_map<int,int> map_; for(int ii=0;ii<n;ii++){ map_[array[ii]]=ii; } for(int ii=0;ii<n;ii++){ int index=map_[array[ii]]; if(index==ii){ if(ii==n-1) printf("%d\n",array[ii]); else printf("%d ",array[ii]); } } } int main(){ int n=0; scanf("%d",&n); if(n<0 ||n>50) return -1; int *array=new int[n]; for(int i=0;i<n;i++){ scanf("%d",array+i); if(array[i]<0 || array[i]>1000) return -1; } reomveOverLapStrs(array, n); delete []array; return 0; }