Apparently Random Numbers - Array

describe

Obviously, NN random integers between 1 and 500 are generated. Please delete the repeated numbers, that is, keep only one of the same numbers, remove the rest of the same numbers, and then sort these numbers from small to large, and output them in the order they are arranged.

Data range: 1 \le n \le 1000 \1≤n≤1000 , the size of the input number satisfies 1 \le val \le 500 \1≤val≤500 

Enter a description:

The first line first inputs the number N of random integers. Enter an integer in each of the next N lines, representing a random number that is clearly generated. For the specific format, please refer to the "Example" below.

Output description:

Output multiple lines, representing the result of input data processing

Example 1

enter:

3
2
2
1

output:

1

illustrate:

Input explanation: 
The first number is 3, that is, N=3 of this small sample, which means that three random integers between 1 and 500 are generated by computer, and then each line has a random number, a total of 3 lines. That is, these 3 random numbers are: 
2 
2 
1 
, so the output of the sample is: 
1 
2      

code:

1) Java solution example (does not mean optimal)

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=Integer.parseInt(in.nextLine());
        List<Integer> list=new ArrayList<>();
        for(int i=0;i<n;i++){
            int x=Integer.parseInt(in.nextLine());
            if(!list.contains(x)){
                list.add(x);
            }
        }
        list.sort(null);
        for(int i:list){
            System.out.println(i);
        }
    }
}

 2) C language solution example (does not mean optimal)

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
    int n;
    
    int a;
    while(~scanf("%d", &n))
    {
        int count[1001] = {0};
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &a);
            count[a] = 1;
        }
        for (int i = 0; i < 1001; i++)
        {
            if (count[i] == 1)
            {
                printf("%d\n", i);
            }
        }
    }
    
    return 0;
}

3) Python language solution (does not mean optimal)

import sys
while True:
    try:
        N=int(raw_input().strip())
        lines=[]
        res=[]
        for i in range(N):
            lines.append(sys.stdin.readline())
        for i in lines:
            if int(i.strip()) not in res:
                res.append(int(i.strip()))
        res.sort()
        for j in res:
            print j
    except EOFError:
        break

Guess you like

Origin blog.csdn.net/qq_43780761/article/details/127115661