Java combat 9: party program list

Java combat 9: party program list

The 11th Blue Bridge Cup 10.

Original title:
[Problem description]
Xiao Ming wants to organize a party for a TV, and a total of n programs have been prepared. Then the time of the party is limited, and he can only choose m programs among them.
These n programs are given in the order Xiaoming imagined, and the order cannot be changed.
Xiao Ming found that the audience’s liking for the evening has a very large relationship with the goodness of the first few programs. He hopes that the first program selected is as good as possible. Under this premise, he hopes that the second program is as good as possible. analogy.
Xiao Ming defines a good-looking value for each program. Please help Xiao Ming choose m programs to meet his requirements.
[Input format]
The first line of input contains two integers n, m, indicating the number of programs and the number to be selected.
The second line contains n integers, which are the good-looking values ​​of each program in turn.
[Output format] The
output line contains m integers, which is the good-looking value of the selected program.
[Sample input]
5 3
3 1 2 5 4
[Sample output]
3 5 4
[Sample description]
The 1, 4, and 5 programs are selected.
[Evaluation use case scale and conventions]
For 30% of the evaluation cases, 1 <= n <= 20;
for 60% of the evaluation cases, 1 <= n <= 100;
for all evaluation cases, 1 <= n <= 100000, 0 <= The good-looking value of the program <= 100000.

Idea: After sorting, output in the original order, which is easy.

code show as below:

import java.util.Scanner;
public class blueSelf_10 {
    
    
    public static void main(String[] args){
    
    
        Scanner reader=new Scanner(System.in);
        int n=reader.nextInt();
        int a[]=new int[n];
        int b[]=new int[n];
        int m=reader.nextInt();
        for(int i=0;i<n;i++){
    
    
            a[i]=reader.nextInt();
            b[i]=a[i];
        }

        int t;
        for (int i = 1; i < n; i++) {
    
    
            for (int j = 0; j < n-1; j++) {
    
    
                if (b[j]< b[j + 1]) {
    
    
                    t = b[j];
                    b[j] = b[j + 1];
                    b[j + 1] = t;
                }
            }
        }
        for(int i=0;i<n;i++){
    
    
            for (int j = 0; j < m; j++) {
    
    
                if(a[i]==b[j]){
    
    
                    System.out.printf("%d ",a[i]);
                }
            }
        }
    }
}
/*
5 3
3 1 2 5 4

*/

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46020391/article/details/112361222