Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
求n个整数中的绝对值最大的数。
Input
输入数据有2行,第一行为n,第二行是n个整数。
Output
输出n个整数中绝对值最大的数。
Sample Input
5 -1 2 3 4 -5
Sample Output
-5
Hint
Source
package src;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int max = 0;
int flag = 0;
for(int i = 0; i < n; i++)
{
int a = reader.nextInt();
int b;
if(a<0)
{
b = -a;
}
else {
b = a;
}
if(b>max)
{
max = b;
flag = a;
}
}
System.out.println(flag);
}
}