Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
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);
}
}
最后
以上就是烂漫紫菜最近收集整理的关于java求n个数的最大值_java--求绝对值最大值的全部内容,更多相关java求n个数内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复