有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java代码可以打印当前数字右边的最大数字,最后一个数字应该打印1

该代码用于打印当前数字右侧的最大数字,最后一个数字应打印-1

import java.util.Scanner;

public class Maxtoright {
 
    public static void main(String[] args) {        
        Scanner sc = newScanner(System.in);
        int t= sc.nextInt();
        while(t-->0) {
            int n=sc.nextInt();
            int a[]= new int[n];        
                for(int i=0;i<n;i++) {
                    a[i]=sc.nextInt();
                }       
                if(n==1) {          
                    System.out.print("-1");
                }
                if(n==2) {
                    System.out.print(a[n-1]+"-1");
                }       
                if(n>2) {
                for(int i=0;i<n-2;i++) {
                    int max= a[i+1];
                    for(int j=i+1;j<n-2;j++) {
                        if(a[j]>max) {
                            max=a[j];
                        } 
        //              System.out.print(max+" ");
                    }            
                System.out.print(max+" ");
                }       
            System.out.print(a[n-1]+" "+"-1");
        }
    }
}

区块报价


共 (2) 个答案

  1. # 1 楼答案

    据我所知,你有一个未排序的数组,比如: [1, 10, 20, 18, 30] 然后,您希望用户获得用户输入,即数组中的一个数字,并找到其右侧的最大数字。例如,如果输入是10,它将输出30,但如果是最后一个数字,它将只输出-1

    import java.util.Scanner;
    
    class Main {
        public static void main(String[] args) {
            //An unsorted array
            final int[] array = {1, 10, 20, 18, 30};
    
            //Here we get the user input
            final var scanner = new Scanner(System.in);
            final var input = scanner.nextInt();
    
            System.out.println(findBiggestValue(array, input));
        }
    
        private static int findBiggestValue(int[] array, int input){
            //Find index of the input in the array
            var index = -1;
            for(int x = 0; x < array.length; x++){
                var entry = array[x];
                if(entry == input){
                    index = x;
                    break;
                }
            }
    
            //If no index is found, throw an exception
            if(index == -1){
                throw new RuntimeException("The number inserted isn't in the array!");
            }
    
            //If the index is the last entry return -1
            if(index == array.length - 1){
                return -1;
            }
    
            //Find biggest value after the index that we found previously
            var maxValue = input;
            for(var entry : array){
                if(entry > maxValue){
                    maxValue = entry;
                }
            }
    
            return maxValue;
        }
    }
    
  2. # 2 楼答案

    如果要求数组中的每个元素都找到右边最大的元素。(最后一个元素为-1)

    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int t = sc.nextInt();
            while (t  > 0) {
                int n = sc.nextInt();
                int[] input = new int[n];
                for (int i = 0; i < n; i++) {
                    input[i] = sc.nextInt();
                }
                if (n == 1) {
                    System.out.print("-1");
                }
                if (n == 2) {
                    System.out.print(input[n - 1] + "-1");
                } else {
                    findLargeNumber(input);
                }
            }
        }
    
        public static void findLargeNumber(int[] input) {
            int n = input.length;
            int max = input[n - 1];
            System.out.print(input[n - 1] + " " + -1 + "\n");
    
            for (int i = n - 2; i >= 0; i ) {
                int temp = input[i];
                if (temp > max) {
                    max = temp;
                }
                System.out.print(input[i] + " " + max + "\n");
            }
        }
    
    Output: for input {2, 4, 3, 7, 19, 7, 23, 1, 10, 17};
    
    17 -1
    10 17
     1 17
    23 23
     7 23
    19 23
     7 23
     3 23
     4 23
     2 23