实验7-1-2 求最大值及其下标(20 分)

实验7-1-2 求最大值及其下标(20 分)

本题要求编写程序,找出给定的$n$个数中的最大值及其对应的最小下标(下标从0开始)。

输入格式:

输入在第一行中给出一个正整数$n(1\lt n \leq 10)$。第二行输入$n$个整数,用空格分开。

输出格式:

在一行中输出最大值及最大值的最小下标,中间用一个空格分开。

输入样例:

1
2
6
2 8 10 1 9 10

输出样例:

1
10 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h> 
int main( ) {
int i, index, n, temp;
int a[10];

scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
index=0;
a[index]=a[0];
for(i=1;i<n;i++){
if(a[index]<a[i]){
a[index]=a[i];
index=i;
}

}
printf("%d %d\n", a[index],index);
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×