zoukankan      html  css  js  c++  java
  • 蓝桥杯——寻找数组中的最大值

    问题描述

      对于给定整数数组a[],寻找其中最大值,并返回下标。
    输入格式
      整数数组a[],数组元素个数小于1等于100。输入数据分作两行:第一行只有一个数,表示数组元素个数;第二行为数组的各个元素。
    输出格式
      输出最大值,及其下标
    样例输入
    3
    3 2 1
    样例输出
    3 0
    答案:
      

    #include <stdio.h>

    #include <string.h>

    int main(int argc, const char * argv[]) {

        int n;

        scanf("%d",&n);

        

        //输入数据

        int a[n];

        int i;

        for (i = 0;i < n;++i) {

            scanf("%d",&a[i]);

        }

        

        //确定最大值

        int max = a[0];

        for (i = 1; i <n;i ++) {

            if (max <a[i]) {

                max = a[i];

            }

        }

        

        //输入数组中的最大值和相应索引

        printf("%d ",max);

        for (i = 0; i < n; i ++) {

            if (max == a[i]) {

                printf("%d",i);

            }

        }

        

        return 0;

    }

  • 相关阅读:
    day24<多线程>
    day23<File类递归练习>
    day22<IO流+>
    day21<IO流+&FIle递归>
    day20<IO流>
    day19<异常&File类>
    day18<集合框架+>
    day17<集合框架+>
    R语言中的标准输入,输出, 错误流
    Perl Spreadsheet::WriteExcel 模块自动生成excel 文件
  • 原文地址:https://www.cnblogs.com/123qw/p/4383911.html
Copyright © 2011-2022 走看看