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

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

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

输入格式:

输入在第一行中给出一个正整数n(1<n10)。第二行输入n个整数,用空格分开。

输出格式:

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

输入样例:

代码:


#include <stdio.h>
main()
{
  int n,i,j;
  scanf("%d",&n);
  int a[11];
  for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  for(j=0;j<n;j++)
 {
      if(a[0]<a[j])
     {
        a[0]=a[j];
        i=j;
       }
      else if(a[0]==a[j])
      {
        a[0]=a[j];
        if(i>j)
        {
            i=j;
           }
        }
  }
  printf("%d %d",a[0],i);
}

猜你喜欢

转载自www.cnblogs.com/20188703zyj/p/10466304.html