求绝对值最大值java
Problem Description
求n个整数中的绝对值最大的数。
Input
输入数据有2行,第一行为n,第二行是n个整数。
Output
输出n个整数中绝对值最大的数。
Sample Input
5
-1 2 3 4 -5
Sample Output
-5
Hint
Source
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner str = new Scanner(System.in);
int n, max, a;
n = str.nextInt();
max = 0;
for(int i = 0; i < n; i++)
{
a = str.nextInt();
if(Math.abs(max) < Math.abs(a)) max = a;
}
System.out.println(max);
str.close();
}
}
求绝对值Math.abs()
还没有评论,来说两句吧...