http://lx.lanqiao.org/problem.page?gpid=T111

 算法训练 寻找数组中最大值  
时间限制:1.0s   内存限制:512.0MB
    
问题描述
  对于给定整数数组a[],寻找其中最大值,并返回下标。
输入格式
  整数数组a[],数组元素个数小于1等于100。输出数据分作两行:第一行只有一个数,表示数组元素个数;第二行为数组的各个元素。
输出格式
  输出最大值,及其下标
样例输入
33 2 1
样例输出
3 0
 
分析:
 
变量标记即可。
 
AC代码:
 
 1 #include <stdio.h>
 2 
 3 const int INF = 0x3f3f3f3f;
 4 
 5 int main()
 6 {
 7     int n , i , temp , max = -INF , ans;
 8     scanf("%d",&n);
 9     if(n <= 0)
10         return 0;
11     for(i = 0;i < n;i ++)
12     {
13         scanf("%d",&temp);
14         if(temp > max)
15         {
16             max = temp;
17             ans = i;
18         }
19     }
20     printf("%d %d\n",max , ans);
21     return 0;
22 }
View Code

 

相关文章:

  • 2022-02-08
  • 2021-11-04
  • 2021-07-20
  • 2022-12-23
猜你喜欢
  • 2021-10-17
  • 2021-09-19
  • 2022-12-23
  • 2022-01-05
  • 2021-09-09
  • 2021-10-17
相关资源
相似解决方案