输入一个 int 型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。
保证输入的整数最后一位不是 0 。
数据范围:
#include<iostream> using namespace std; int main() { int n; int a[10]={0}; int num=0; cin>>n ; while(n) { if(a[n%10]==0) { a[n%10]++;//这一步是更新,遇到下次相同的数会跳过 num=num*10+n%10; } n/=10; } cout<<num<<endl; return 0; }
#include<iostream> using namespace std; int main() { int num; cin>>num; bool flag[10]={0}; while(num) { if(flag[num%10]==0) cout<<(num%10); flag[num%10]=1; num/=10; } }
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); String str = String.valueOf(sc.next()); HashMap<Character,Integer> map = new HashMap<Character,Integer>(); int len = str.length()-1; StringBuilder sb = new StringBuilder(); for(int i = len ; i >= 0;i--){ if(map.containsKey(str.charAt(i))){ continue; } sb.append(str.charAt(i)); map.put(str.charAt(i),1); } int ans = Integer.valueOf(sb.toString()); System.out.println(sb); } }
import java.util.Scanner; /** * @author aiker * @since 2021/11/6 */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { StringBuilder sb = new StringBuilder(); int nextInt = scanner.nextInt(); String number = String.valueOf(nextInt); for (int i = number.length(); i > 0; i--) { String substring = number.substring(i - 1, i); if (sb.indexOf(substring) == -1) { // 不包含 sb.append(substring); } } System.out.println(Integer.parseInt(sb.toString())); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); StringBuffer sb = new StringBuffer(sc.nextLine()); sb.reverse(); String out=""; for (int i = 0; i < sb.length(); i++) { if (!out.contains(sb.substring(i, i+1))) { out+=sb.substring(i, i+1); } } System.out.println(out); } }
利用标准库set的无重复特性,一次循环输出字符即可 #include <iostream> #include <set> #include <string> using namespace std; int main(void) { string strCin; getline(cin, strCin); set<char> arrNumber; for (auto it = strCin.rbegin(); it != strCin.rend(); it++) { bool bRet = arrNumber.insert(*it).second; if (bRet) { cout << *it; } } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String input; while((input = reader.readLine())!=null){ while(input.charAt(input.length()-1)=='0'){ input = input.substring(0,input.length()-1); } //建立映射数组 int[] map = new int[10]; for(int i=input.length()-1;i>=0;i--){ //记录出现次数 map[input.charAt(i)-'0']++; //首次出现sh if(map[input.charAt(i)-'0']==1){ System.out.print(input.charAt(i)); } } } } }
import java.util.Scanner; public class Main{ public static void main(String [] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ int i = in.nextInt(); int [] buckets = new int[10]; char [] chs = String.valueOf(i).toCharArray(); StringBuffer sb = new StringBuffer(); int index; for(int j = chs.length-1;j>=0;--j){ index = Integer.valueOf(chs[j]+""); if (buckets[index]>0){ continue; }else { sb.append(chs[j]); buckets[index]++; } } // sb.reverse();//过程已经把顺序处理好,无需再做反转 System.out.println(sb.toString()); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Stack<Integer> stack = new Stack<>(); while(n>0){ if(stack.search(n%10)==-1) stack.add(n%10); n/=10; } stack.forEach(System.out::print); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int num = input.nextInt(); String s = num+""; Map<Character,Integer> map = new HashMap<Character,Integer>(); for(int i=0; i<s.length(); i++){ if(!map.containsKey(map.get(s.charAt(i)))){ map.put(s.charAt(i),1); } } for(int i=s.length()-1; i>=0; i--){ if(map.containsKey(s.charAt(i))){ System.out.print(s.charAt(i)); map.remove(s.charAt(i)); } } } }用了HashMap