求绝对值最大值java

àì夳堔傛蜴生んèń 2022-05-30 11:28 365阅读 0赞

Problem Description

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

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

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

5
-1 2 3 4 -5
Sample Output

-5
Hint

Source

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner str = new Scanner(System.in);
  5. int n, max, a;
  6. n = str.nextInt();
  7. max = 0;
  8. for(int i = 0; i < n; i++)
  9. {
  10. a = str.nextInt();
  11. if(Math.abs(max) < Math.abs(a)) max = a;
  12. }
  13. System.out.println(max);
  14. str.close();
  15. }
  16. }

求绝对值Math.abs()

发表评论

表情:
评论列表 (有 0 条评论,365人围观)

还没有评论,来说两句吧...

相关阅读