东方博宜OJ 1683:递归法求最大值 ← 递归

【题目来源】
https://oj.czos.cn/p/1683

【题目描述】
请使用递归求 N 个数中的最大数及其位置。

【输入格式】
第一行一个整数 N,N≤1000。
第二行,N 个不重复的整数。

【输出格式】
最大值和其位置。

【输入样例】
10
2 4 1 6 3 10 9 7 8 5

【输出样例】
10 6

【数据范围】
N≤1000

【算法分析】
● 要善于变通,要学会从不同角度进行思考,来构建递归函数。

【算法代码】

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

const int maxn=1e3+5;
int a[maxn];

int f(int n) { //求前n个数的最大值的下标
    if(n==1) return 1;
    int x=f(n-1);
    if(a[x]>a[n]) return x;
    else return n;
}

int main() {
    int n;
    cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>a[i];
    }

    cout<<a[f(n)]<<" "<<f(n);

    return 0;
}

/*
in:
10
2 4 1 6 3 10 9 7 8 5

out:
10 6
*/

【参考文献】
https://blog.csdn.net/hnjzsyjyj/article/details/156211550
https://blog.csdn.net/hnjzsyjyj/article/details/156206151
https://blog.csdn.net/hnjzsyjyj/article/details/156206554
https://blog.csdn.net/hnjzsyjyj/article/details/156201255
https://blog.csdn.net/hnjzsyjyj/article/details/156192874
https://blog.csdn.net/hnjzsyjyj/article/details/156204715
https://blog.csdn.net/hnjzsyjyj/article/details/156185382
https://blog.csdn.net/hnjzsyjyj/article/details/156182882
https://www.bilibili.com/video/BV1TW4y1K7YK/

相关推荐
2401_841495642 天前
【LeetCode刷题】对称二叉树
数据结构·python·算法·leetcode·二叉树··递归
2401_841495644 天前
【LeetCode刷题】翻转二叉树
python·算法·leetcode··递归·节点·翻转二叉树
闻缺陷则喜何志丹5 天前
【栈 递归】P8650 [蓝桥杯 2017 省 A] 正则问题|普及+
c++·数学·蓝桥杯·递归·
少许极端6 天前
算法奇妙屋(二十七)-全排列与子集问题
算法·剪枝·回溯·递归
老鼠只爱大米7 天前
LeetCode经典算法面试题 #98:验证二叉搜索树(递归法、迭代法等五种实现方案详解)
算法·leetcode·二叉树·递归·二叉搜索树·迭代
2401_841495649 天前
【LeetCode刷题】二叉树的中序遍历
数据结构·python·算法·leetcode··递归·遍历
卷卷的小趴菜学编程10 天前
算法篇----递归回溯
c++·算法·递归·回溯算法·暴力搜索·floodfill算法·二叉树深搜
程序员-King.13 天前
day167—递归—二叉树的直径(LeetCode-543)
算法·leetcode·深度优先·递归
程序员-King.13 天前
day168—递归—二叉树的最大路径和(LeetCode-124)
算法·leetcode·深度优先·递归
老鼠只爱大米14 天前
LeetCode经典算法面试题 #24:两两交换链表中的节点(迭代法、递归法等多种实现方案详细解析)
算法·leetcode·链表·递归·双指针·迭代·链表交换