美文网首页
算法训练 寻找数组中最大值

算法训练 寻找数组中最大值

作者: DongBold | 来源:发表于2017-03-04 16:28 被阅读70次

    问题描述

    对于给定整数数组a[],寻找其中最大值,并返回下标。

    输入格式

    整数数组a[],数组元素个数小于1等于100。输出数据分作两行:第一行只有一个数,表示数组元素个数;第二行为数组的各个元素。

    输出格式

    输出最大值,及其下标

    样例输入

    3
    3 2 1

    样例输出

    3 0

    水题

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        int n, temp;
        int index = -1, maxNum = INT_MIN;
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            scanf("%d", &temp);
            if (maxNum < temp) {
                maxNum = temp;
                index = i;
            }
        } 
        printf("%d %d\n", maxNum, index);
        
        return 0;
    } 
    

    相关文章

      网友评论

          本文标题:算法训练 寻找数组中最大值

          本文链接:https://www.haomeiwen.com/subject/ozljgttx.html