首页 > 试题广场 >

求最大最小数

[编程题]求最大最小数
  • 热度指数:23035 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入N个(N<=10000)数字,求出这N个数字中的最大值和最小值。每个数字的绝对值不大于1000000。

输入描述:
输入包括多组测试用例,每组测试用例由一个整数N开头,接下去一行给出N个整数。


输出描述:
输出包括两个整数,为给定N个数中的最大值与最小值。
示例1

输入

5
1 2 3 4 5
3
3 7 8

输出

5 1
8 3
import java.util.*;
public class Main {
    public static void main(String[] args) {
        // write your code here
       Scanner scan=new Scanner(System.in);
       while(scan.hasNext()){
           int n=scan.nextInt();
           int res[]=new int[n];
           for(int i=0;i<n;i++){
               res[i]=scan.nextInt();
           }
           Arrays.sort(res);
           System.out.println(res[n-1]+" "+res[0]);
       }

    }
}

发表于 2021-01-22 07:32:30 回复(0)
  • 没有排序,没有改变数组的结构,只是把最大最小值存放在临时变量当中
    import java.util.*;
    public class Main
    {
      public static void main(String [] args)
      {
          Scanner sc=new Scanner(System.in);
          while(sc.hasNextInt())
          {
              int n=sc.nextInt();
              int [] array=new int[n];
              for(int i=0;i<n;i++)
              {
                  array[i]=sc.nextInt();
              }
              int max=array[0];
              for(int i=1;i<array.length;i++)
              {
                  if(array[i]>max)
                  {
                      max=array[i];
                  }
              }
              int min=array[0];
              for(int i=1;i<array.length;i++)
              {
                  if(array[i]<min)
                  {
                      min=array[i];
                  }
              }
              System.out.println(max+" "+min);
          }
      }
    }
发表于 2020-02-22 11:05:34 回复(0)
import java.util.Scanner;
import java.util.Arrays;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int[] nums = new int[n];
            for(int i=0;i<n;i++){
                nums[i] = sc.nextInt();
            }
            Arrays.sort(nums);
            System.out.println(nums[n-1]+" "+nums[0]);
        }
    }
}

发表于 2018-09-19 08:55:16 回复(0)

给一个基本都会的一个思路,虽然时间复杂度、空间复杂度不够优秀,但还是能解题的QAQ

import java.util.Scanner;

public class Main {
    static int biggest(int[] a) {
        int max = a[0];
        for(int i = 1; i < a.length; i++) {
            if(a[i] > max) {
                max = a[i];
            }
        }
        return max;
    }
    static int smallest(int[] a) {
        int min = a[0];
        for(int i = 1; i < a.length; i++) {
            if(a[i] < min) {
                min = a[i];
            }
        }
        return min;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()) {
            int n = scan.nextInt();
            int[] arr = new int[n];
            for(int i = 0; i < n; i++) {
                arr[i] = scan.nextInt();
            }
            System.out.println(biggest(arr) + " " + smallest(arr));
        }
    }
}
发表于 2018-04-15 12:59:41 回复(0)

问题信息

难度:
4条回答 18755浏览

热门推荐

通过挑战的用户

查看代码