c ++递归算法数的计数

Given an array of length N and an integer x, you need to find and return the last index of integer x present in the array. Return -1 if it is not present in the array. Last index means - if x is present multiple times in the array, return the index at which x comes last in the array.

给定长度为N和整数x的数组,您需要查找并返回数组中存在的整数x的最后一个索引。 如果数组中不存在,则返回-1。 Last index表示-如果x在数组中多次出现,则返回x在数组中最后出现的索引。

You should start traversing your array from 0, not from (N - 1). Do this recursively. Indexing in the array starts from 0.

您应该从0开始遍历数组,而不是从(N-1)开始遍历数组。 递归执行此操作。 数组中的索引从0开始。

Input Format:

输入格式:

  • Line 1 : An Integer N i.e. size of array

    第1行:整数N,即数组的大小

  • Line 2 : N integers which are elements of the array, separated by spaces

    第2行: N个整数,它们是数组的元素,以空格分隔

  • Line 3 : Integer x

    第3行:整数x

Output Format: last index or -1

输出格式:最后一个索引或-1

Constraints: 1 <= N <= 10^3

限制条件: 1 <= N <= 10 ^ 3

Example

    Input:
4
9 8 10 8
8
Output:
3

Description:

描述:

Here, we have to find the last occurrence of x. Therefore, in this example the last occurrence of 8 happens in index 3, and hence the output is 3.

在这里,我们必须找到x的最后一次出现。 因此,在此示例中,最后一次出现8发生在索引3中,因此输出为3。

Algorithm:

算法:

Step 1: To solve this using recursion, make a recursion function with inputs, and a variable currIndex to traverse the input array.

步骤1:要使用递归解决此问题,请使用输入创建递归函数,并使用变量currIndex遍历输入数组。

Step2: Base Case:- If currIndex == size of the input array, return -1, i.e element not found.

步骤2:基本情况:-如果currIndex ==输入数组的大小,则返回-1,即找不到元素。

Step3: Take input of next recursion call ,withcurrIndex incremented by 1 , in a variable ‘index’.

步骤3:在变量“ index”中,获取下一个递归调用的输入,其currIndex递增1。

Step 4:
If(index == -1 && input[currIndex] == x)
Return currIndex
Else
Return index;

第4步:
If(索引== -1 &&输入[currIndex] == x)
返回currIndex
其他
返回索引;

C ++源代码/功能: (C++ Source Code/Function:)

#include<bits/stdc++.h>
using namespace std;
int lastIndex(int input[], int size, int x, int currIndex){if(currIndex== size){return -1;
}
int index = lastIndex(input,size,x,currIndex+1);
if(index == -1 && input[currIndex] == x){return currIndex;
}
else{return index;
}
}
int main(){int input[] = {9,8,10,8};
int x = 8;
int size = 4;
cout<<lastIndex(input,size,x,0);
return 0;
}

Output

输出量

3

翻译自: https://www.includehelp.com/cpp-programs/find-last-occurrence-of-a-number-using-recursion-in-an-array.aspx

c ++递归算法数的计数

c ++递归算法数的计数_C ++程序使用数组中的递归查找数字的最后一次出现相关推荐

  1. c语言++数组名【数字】_C ++程序在数组中打印所有非重复数字

    c语言++数组名[数字] Problem statement: Write a C++ program to print all the non-repeated numbers in an arra ...

  2. c ++递归算法数的计数_计数排序算法–在C / C ++中实现的想法

    c ++递归算法数的计数 What is the counting sort algorithm? In Computer Science, sorting algorithms form the b ...

  3. 《程序员代码面试指南》第七章 位运算 在其他数都出现k 次的数组中找到只出现一次的数...

    题目 在其他数都出现k 次的数组中找到只出现一次的数 java 代码 package com.lizhouwei.chapter7;/*** @Description: 在其他数都出现k 次的数组中找 ...

  4. C语言—数组,给定如下的数组: char chars[] = { ‘a‘, ‘ ‘, ‘b‘, ‘ ‘, ‘c‘, ‘ ‘, ‘ ‘, ‘d‘ } ;写一个程序将数组中所有的空格字符替换为下划线字符‘

    给定如下的数组:   char chars[] = { 'a', ' ', 'b', ' ', 'c', ' ', ' ', 'd' } ; 写一个程序将数组中所有的空格字符替换为下划线字符'_'. ...

  5. 【程序填空题】查最贵的书和最便宜的书。【问题描述】编写程序,从键盘输入n(n<10)种书的名称和定价并存入结构体数组中,从中查找定价最高及最低的书名和定价,并输出。【输入形式】先输入书

    [程序填空题]查最贵的书和最便宜的书. [问题描述] 编写程序,从键盘输入n(n<10)种书的名称和定价并存入结构体数组中,从中查找定价最高及最低的书名和定价,并输出. [输入形式] 先输入书总 ...

  6. 反转字符串中的元音字符_C程序消除字符串中的所有元音

    反转字符串中的元音字符 Given a string and we have to eliminate/ remove all vowels from the string using C progr ...

  7. 在其他数都出现k次的数组中找到只出现一次的数

    题目 给定一个整型数组arr和一个大于1的整数k,已知arr中只有一个数出现了一次,其他的数都出现了k次,请返回只出现1次的数.要求时间复杂度O(N),空间复杂度O(1). 基本思路 首先看一个七进制 ...

  8. 在其他数都出现偶数次的数组中找到出现奇数次的数

    题目 给定一个数组arr,其中只有一个数出现了奇数次,其他数都出现了偶数次,打印这个数. 进阶问题 有两个数出现了奇数次,其他数出现了偶数次,打印这两个数. 要求 时间复杂度O(N),空间复杂度(1) ...

  9. C语言经典题目——将一个数插入已排序好的数组中

    <1>题目介绍 有一个已排好序的数组,要求输入一个数后,按原来的规律将他插入数组中,例如将3插入 1 ,2 ,4 ,7 ,8, 9 ,10 ,11, 13, 100中,得到1 ,2 ,3, ...

最新文章

  1. 【只需4步】windows server系统下快速安装绿色版apache-tomcat-8.0.35(免安装版)
  2. Spring Boot 整合 Spring Security 示例
  3. 网络巨头秘修域名重大疏漏 互联网免遭黑客控制
  4. 真叫人头秃!Python也有pdb
  5. Java语言语法语义分析器设计与实现
  6. Angular本地数据存储LocalStorage
  7. python D29 socketserver以及FTB
  8. springboot指定属性返回_SpringBoot中必须掌握的45个注解
  9. 【转】Linux 命令行下的好东西:一些常用指令
  10. 参数调优为什么要采样_优化参数
  11. [翻译]WPF控件库 MaterialDesignInXamlToolkit (2) Brush Names
  12. 软件著作权申请表怎么填
  13. PNG-的IDAT解析
  14. 词霸天下---140 词根 【-us- = -uti(l)- = -ut- 使用 】
  15. c语言void要用什么头文件,什么是C语言头文件?
  16. 【HTML5】网页实用技巧3:将方形图片设置成圆形后,添加圆形虚线边框
  17. 计算机里硬盘图标,两妙方轻松更改电脑硬盘盘符的图标
  18. html5生成excel,H5纯前端生成Excel表格
  19. 攻防世界新手Misc writeup
  20. Python安装教程步骤3:Pycharm和Anaconda3安装及环境配置相关问题汇总

热门文章

  1. npm ERR! the command again as root/Administrator
  2. 处理网络请求qs、图片转base64的优劣
  3. linux下bios设置内存电压,系统安装的BIOS设置?
  4. svm核函数gamma参数_非线性SVM与核函数
  5. 我所知道的前端组件化与模块化
  6. Echarts在手机端y轴数据过大,显示不全
  7. 自定义checkbox样式
  8. 2018移动端页面适配-自适应最新方案直接写px--------通过gulp工作流搭建一体化的移动端开发环境
  9. 2017年最新基于Bootstrap 4 的专业、多用途响应式布局的系统模板
  10. 笔记36 Spring Web Flow——配置