首页 > 试题广场 >

明明的随机数

[编程题]明明的随机数
  • 热度指数:1553572 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

明明生成了N个1到500之间的随机整数。请你删去其中重复的数字,即相同的数字只保留一个,把其余相同的数去掉,然后再把这些数从小到大排序,按照排好的顺序输出。

数据范围: ,输入的数字大小满足

输入描述:
第一行先输入随机整数的个数 N 。
接下来的 N 行每行输入一个整数,代表明明生成的随机数。
具体格式可以参考下面的"示例"。


输出描述:

输出多行,表示输入数据处理后的结果

示例1

输入

3
2
2
1

输出

1
2

说明

输入解释:
第一个数字是3,也即这个小样例的N=3,说明用计算机生成了3个1到500之间的随机整数,接下来每行一个随机数字,共3行,也即这3个随机数字为:
2
2
1
所以样例的输出为:
1
2       
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int[] arr = new int[a];
        while (in.hasNextInt()) {
       int b = in.nextInt();
// 利用数组下标排序
            arr[b-1] = b;
        }
        for(int i = 0; i < arr.length; i++){
            if( arr[i]>0){
                System.out.println(arr[i]);
            }
        }
    }
}
发表于 2024-05-17 11:16:46 回复(0)
import java.util.Scanner;
import java.util.Scanner;
import java.util.Arrays;
import java.util.TreeSet;

public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

while (scan.hasNext()) {
//接收随机数和数组
int a = scan.nextInt();
int[] arr = new int[a];
for (int i = 0; i < a; i++) {
arr[i] = scan.nextInt();
}

//将数组转换为 set
TreeSet<Integer> sortedSet = new TreeSet<>();
//去重
for (int num : arr) {
sortedSet.add(num);
}

//将去重后的数据给到数组arr1
int arr1[] = new int[sortedSet.size()];
int index = 0;
for (int num1 : sortedSet) {
arr1[index++] = num1;
}

Arrays.sort(arr1);
for (int j = 0; j < arr1.length; j++) {
System.out.println(arr1[j]);
}
}

}
}

发表于 2024-05-15 10:24:51 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        List<Integer> numbers = new ArrayList<>();
        for(int i = 0; i < num; i++) {
            numbers.add(input.nextInt());
        }
        numbers.sort(Comparator.comparingInt(x -> x));
        numbers.stream().distinct().forEach(System.out::println);
    }
}

发表于 2024-05-04 15:54:38 回复(1)
import java.util.Scanner;
import java.util.TreeSet;
import java.util.Iterator;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count = in.nextInt();
        TreeSet<Integer> set = new TreeSet<>();
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt() && count > 0) { // 注意 while 处理多个 case
            int a = in.nextInt();
            set.add(a);
            count--;
        }
        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        
    }
}
发表于 2024-05-01 12:05:23 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 记录N个随机数
        int N = in.nextInt();
        // new一个数组
        int[] num = new int[N];
        for(int i = 0; i < N; i++){
            num[i] = in.nextInt();
        }
        // 先排序后判断
        int[] sortNum = sort(num);
        // 去重
        int temp = sortNum[0];
        int p =1;
        System.out.println(temp);
        while(p < sortNum.length ){
         
            if(sortNum[p] == temp){
                p++;
            }else{
                temp = sortNum[p];
                System.out.println(temp);
                p++;
            }
        }
        }

        // 折半插入排序
        public static int[] sort(int[] num){
            int temp ;
            for(int i = 1 ;i < num.length ; i++){
                int left = 0;
                int right = i - 1;
                temp = num[i];
                while(left <= right){
                    int mid = (left + right)/2;
                    if(num[mid] <= temp){
                        left = mid + 1;
                    }else{
                        right = mid -1;
                    }
                }
                // 执行插入
                for(int j = i -1;j > right;j--){
                    num[j+1] = num[j];
                }
                num[right + 1] = temp;
                
            }
            return num;
        }
    }

发表于 2024-04-27 00:48:25 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Set<Integer> setVal = new HashSet<Integer>();
        in.nextInt();
        // 获取到数组的数量
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            setVal.add(in.nextInt());
        }
        // int total = setVal.size();
        Object[] list = setVal.toArray();
        Arrays.sort(list);
        for(int i = 0;i<list.length;i++){
            System.out.println(list[i]);
        }
    }
}
发表于 2024-04-25 11:14:54 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNextInt()) {
            int n = scan.nextInt();
            HashSet numSet = new HashSet(n);
            while (n > 0) {
                int num = scan.nextInt();
                numSet.add(num);
                n--;
            }
            numSet.stream().sorted().forEach(System.out::println);
        }
    }
}
编辑于 2024-04-09 12:16:49 回复(0)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class  Main{

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        //个数
        int a = in.nextInt();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < a; i++) {
            list.add(in.nextInt());
        }

      list=list.stream().distinct().sorted().collect(Collectors.toList());
         for (Integer i:list) {
            System.out.println(i);
        }
    }
}
编辑于 2024-04-04 12:38:41 回复(0)
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int n = in.nextInt();
TreeSet<Integer> treeSet = new TreeSet<>();
for(int i = 0;i<n;i++){
treeSet.add(in.nextInt());
}
for(int i :treeSet){
System.out.println(i);
}
in.close();
}
}
编辑于 2024-04-02 11:02:16 回复(0)
import java.util.Scanner;
import java.util.*;
import java.util.stream.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Set<Integer> set = new HashSet<>();
        int a = in.nextInt();
        while(in.hasNextInt()){
            set.add(in.nextInt());
        }
        List<Integer> result = set.stream().sorted().collect(Collectors.toList());
        result.forEach( p->{
            System.out.println(p);
            }
        );

    }
}
用java就是爽 语法糖
发表于 2024-03-14 22:27:11 回复(1)
import java.util.Scanner;
import java.util.TreeSet;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        TreeSet<Integer> set = new TreeSet();
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            for (int i = 0; i < a; i ++ ) {
                int val = in.nextInt();
                set.add(val);
            }
        }
        set.forEach(System.out::println);
    }
}

编辑于 2024-03-14 22:04:17 回复(0)
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int[] n = new int[1001];

        while (N-- > 0) {
            n[sc.nextInt()] = 1;
        }

        for (int i = 0; i < n.length; i++) {
            if (n[i] == 1) {
                System.out.println(i);
            }
        }
    }
}
第二种Map方式
import java.util.*;
import java.util.stream.Collectors;


public class Main {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int N = in.nextInt();
        Map<Integer, Integer> map = new HashMap<>();
        while (N-- > 0) {
            map.put(in.nextInt(), 0);
        }
        List<Object> collect = Arrays.stream(map.keySet().toArray()).sorted().collect(Collectors.toList());
        for (Object o : collect) {
            System.out.println((int) o);
        }
    }
}


编辑于 2024-03-13 13:13:01 回复(0)
可以借助链表,LinkedList,双链表
1、如果当前值比最后一个值大,那么直接添加至最后面;
2、如果当前值比第一个值小,那么将当前值添加至最前面;
3、其余情况开始循环比较,有重复值结束循环,
找到比当前值大的数,那么就将当前值插入当前下标
发表于 2024-03-09 19:50:56 回复(0)
import java.util.Scanner;
import java.util.HashMap; // 引入 HashMap 类
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int a = in.nextInt();
        Map<Integer,Integer> m1 = new HashMap<>();
        List<Integer> aa = new ArrayList<>();
        while(in.hasNextInt()){
            int b = in.nextInt();
            m1.put(b,b);
        }
        m1.forEach((key,value)->{
            aa.add(value);
        });
        Collections.sort(aa);
        aa.forEach(value->{
            System.out.println(value);
        });
    }
}
编辑于 2024-03-07 15:29:59 回复(0)
只会map了,先将就着,后面再回来看
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < N; i++){
            int rans = in.nextInt();
            if(!map.containsKey(rans)){
                map.put(rans, i);
            }
        }
        Object [] ns = map.keySet().toArray();
        Arrays.sort(ns);
        for(int i = 0; i < ns.length; i++){
            System.out.println(ns[i]);
        }
    }
}

编辑于 2024-03-07 12:36:17 回复(0)
能不能更新一下jdk 啊
这版本太老了吧
很多语法糖都不支持 好难受啊
编辑于 2024-03-05 22:40:29 回复(0)
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Set<Integer> set = new TreeSet<>();

        // 第一行是数字的个数
        int N = Integer.parseInt(in.next()); // 这里应该使用 nextInt 来获取数量 N

        // 根据数量 N,循环读取后续的整数
        for (int i = 0; i < N; i++) {
            if (in.hasNext()) {
                set.add(Integer.parseInt(in.next()));
            }
        }

        set.stream().forEach(System.out::println);

        in.close();
    }
}
发表于 2024-01-19 15:27:29 回复(0)

问题信息

难度:
372条回答 286244浏览

热门推荐

通过挑战的用户

查看代码