简体   繁体   English

已解决:找到输入数组的最大值

[英]Solved: find max of an input array

EDIT:: I was assuming there would be 3 array.编辑:: 我假设会有 3 个数组。 The problem was not asking for that.问题不在于要求。

I am trying to write a program from the platform Jutge.org that reads sequences of integer numbers and prints the maximum value of each sequence.我正在尝试从平台 Jutge.org 编写一个程序,该程序读取 integer 数字序列并打印每个序列的最大值。

Input consists in the input of sequences.输入包括序列的输入。 Each sequence begins with its number of elements n > 0, followed by n integer numbers.每个序列以元素数量 n > 0 开始,然后是 n 个 integer 数字。 Given the numbers and the length of the sequence [] (it will be introduced before the sequence of numbers) the input/output sample looks like this:给定序列 [] 的数字和长度(将在数字序列之前介绍),输入/输出样本如下所示:

Input输入

10 (array length input)  
10 30 40 50 60 30 40 15 10 20

2  (array length input) 
-54 -134

4  (array length input) 
1 1 1 1

Output Output

60
-54
1

I have the exact output, but I think something is missing, because the Judge.org compiler won't accept my code as correct.我有确切的 output,但我认为缺少一些东西,因为 Judge.org 编译器不会接受我的代码是正确的。

import java.util.*;
public class problema7 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int max1=Integer.MIN_VALUE;
        int max2=Integer.MIN_VALUE;
        int max3=Integer.MIN_VALUE;

        // sequence 1

        int num1 = in.nextInt();
        int[] seq1 = new int [num1];

        for(int x = 0; x<num1; x++) {
            seq1[x] = in.nextInt();
            if (max1 == 0 || seq1[x] > max1) max1 = seq1[x];
        }

        // sequence 2

        int num2 = in.nextInt();
        int[] seq2 = new int [num2];

        for(int x = 0; x < num2; x++) {
            seq2[x] = in.nextInt();
            if (max2 == 0 || seq2[x] > max2) max2 = seq2[x];
        }

        // sequence 3

        int num3 = in.nextInt();
        int[] seq3 = new int [num3];

        for(int x = 0; x<num3; x++) {
            seq3[x] = in.nextInt();
            if (max3 == 0 || seq3[x] > max3) max3 = seq3[x];
        }

        System.out.println(max1);
        System.out.println(max2);
        System.out.println(max3);
    }
}

problem:问题:

https://jutge.org/problems/P71753_en/pdf https://jutge.org/problems/P71753_en/pdf

You seem to assume there will be exactly 3 arrays to process;您似乎认为将有 3 个 arrays 需要处理; this is not stated in the problem description.这在问题描述中没有说明。

First of all welcome to SO.首先欢迎来到SO。

Secondly, Scott's observation is correct, are you sure your input will always have 3 lines?其次,斯科特的观察是正确的,你确定你的输入总是有 3 行吗? This seems a bit constrictive.这似乎有点拘束。 Bellow I've made a simple class to illustrate how you could possibly solve this problem:贝娄我做了一个简单的 class 来说明如何解决这个问题:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String line;
        System.out.println("Insert line (empty terminates the program):");

        while(!(line = scanner.nextLine()).isBlank()){
            String[] segments = line.split(" ", 2);

            int population = Integer.parseInt(segments[0]);
            String[] sequence = segments[1].trim().split(" ");
            if(population != sequence.length){
                System.err.println("Population declaration and sequence size do not match\nPopulation: " + population + "\nSequence: "+ segments[1] + ", size: " + sequence[1].length());
                System.exit(0);
            }

            int max = Integer.MIN_VALUE;

            for (String number : sequence) {
                max = Math.max(Integer.parseInt(number), max);
            }

            System.out.println(max);
            System.out.println("Insert line (empty terminates the program):");
        }

        System.out.println("Now exiting");
    }

}

It expects input from the keyboard, splits at the first space in a line, checks if the correct number of numbers is inserted after the first number (the one denoting the population of the following sequence) and then just finds the maximum number and prints it.它期望来自键盘的输入,在一行的第一个空格处分割,检查是否在第一个数字(表示以下序列的人口的那个)之后插入了正确数量的数字,然后找到最大数字并打印它. It will exit if you enter a blank line.如果您输入一个空行,它将退出。

I'd suggest you adapt it for your input.我建议您根据自己的输入进行调整。

Edit: The condition for the while loop uses Java 11's isBlank() method.编辑:while 循环的条件使用 Java 11 的 isBlank() 方法。 So you'll need a Java version >= 11 or you can adjust the condition to be compatible with the version you're using.因此,您需要 Java 版本 >= 11,或者您可以调整条件以与您使用的版本兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM