一、通常状况下 默认第一次输入数做为 总的数目
输入形如
5
12
21
32
11
12
这样的数据处理
//对输入的数字去重并倒序输出 public class testJava2 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int len = scan.nextInt();//将第一次输入的数据做为循环次数 int[] numArr = new int[len]; int index = 0; for (int i = 0; i < len; i++) { numArr[i] = scan.nextInt(); } fun(numArr); } static void fun(int[] numArr){ // 排序 Arrays.sort(numArr); // 用List储存结果 List<Integer> result = new ArrayList<Integer>(); result.add(numArr[0]); // for (int t = 1; t < numArr.length - 1; t++) { if (numArr[t] != numArr[t - 1]) { result.add(numArr[t]); } } // 处理数组中最后一位数字 if (numArr[numArr.length - 1] != numArr[numArr.length - 2]) { result.add(numArr[numArr.length - 1]); } // 输出结果 for (int i = 0; i < result.size(); i++) { System.out.println(result.get(i)); } } }
二、考虑输入时形如 -3322110 这样的数据 要求 输出 -123 即 末尾若是为 0 则去掉,其余位数据去重并倒序 数据 正负不变
public class testJava3 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // String str = scan.nextLine(); String tempStr = "";// 过渡字符 String firstChar = "";// 首位字符 String returnStr = "";// 最终输出字符 // if (str.charAt(0) == '-' || str.charAt(str.length() - 1) == '0') { // 首位处理 if (str.charAt(0) == '-') { // System.out.println("首位处理"); tempStr = str.substring(1, str.length()); firstChar = str.substring(0, 1);// 取出首位 } if (str.charAt(str.length() - 1) == '0') { // 末尾为0处理 tempStr = tempStr.substring(0, tempStr.length() - 1); } } else { tempStr = str; } // 去重处理 List<String> list = new ArrayList<String>(); for (int i = 0; i < tempStr.length(); i++) { // 此种判断未考虑 123123 这种状况 // if (tempStr.charAt(i) != tempStr.charAt(i - 1)) String n = String.valueOf(tempStr.charAt(i)); if (!list.contains(n)) { list.add(n); } } // 反转 for (int i = list.size() - 1; i >= 0; i--) { returnStr += list.get(i); } // 输出 System.out.println(firstChar + returnStr); } }