这7-1 求最大值及其下标 c++

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

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

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

输入样例:
6
2 8 10 1 9 10
输出样例:
10 2

#include<iostream>
using namespace std;
void input(int[],int);
int main()
{
    int a[10];
    int n;
    cin>>n;
    input(a,n);     //读入n个数到a数组中
    int t=0;
    for(int i=1;i<n;i++)
    {
        if(a[i]>a[t])
        t=i;
    }
    cout<<a[t]<<" "<<t;
    return 0;
}
void input(int a[],int n)
{
    for(int i=0;i<n;i++)
    cin>>a[i];
}
发布了12 篇原创文章 · 获赞 0 · 访问量 1220

猜你喜欢

转载自blog.csdn.net/weixin_45644335/article/details/102956071
今日推荐