求绝对值最大值

时间: 2023-07-10 admin IT培训

求绝对值最大值

求绝对值最大值

Problem Description

求n个整数中的绝对值最大的数。

Input

输入数据有2行,第一行为n,第二行是n个整数。

Output

输出n个整数中绝对值最大的数。

Sample Input

5
-1 2 3 4 -5

Sample Output

-5

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
         int a = input.nextInt();
         int i;
         int h[]=new int[a];
         for(i=0;i<a;i++)
         {
             h[i] = input.nextInt();
         }
         int f = h[0];
         for(i=0;i<a;i++)
         {
             if(Math.abs(h[i])>Math.abs(f))f=h[i];
         }
         System.out.println(f);
          input.close();
    }
}