题目描述
本题要求编写程序,找出给定的n个数中的最大值及其对应的最小下标(下标从0开始)。
输入格式
输入在第一行中给出一个正整数n(1<n≤10)。第二行输入n个整数,用空格分开。
输出格式
在一行中输出最大值及最大值的最小下标,中间用一个空格分开。
输入样例复制
在这里给出一组输入。例如:
6
2 8 10 1 9 10
输出样例复制
在这里给出相应的输出。例如:
10 2
代码如下
public static void MaxNum(){
int a[] = null;
Scanner scan = new Scanner(System.in);
int n,j = 0;
int max = 0;
n = scan.nextInt();
a = new int [n];
for (int i = 0; i < n; i++) {
a[i] = scan.nextInt();
}
for (int i = 0; i < n; i++) {
if(a[i] > max){
max = a[i];
j = i;
}
}
System.out.println(max + "," + j);
}
结尾
以上是今天的内容,如有不对处,评论区交流。