Recursive function
5-1 已知A[n]为整数数组,试写出实现下列运算的递归算法:
(1) 求数组A中的最大整数。
(2) 求n个整数的和。
(3) 求n个整数的平均值。
【解答】
#include <iostream.h>class RecurveArray { //数组类声明private: int *Elements; //数组指针 int ArraySize; //数组尺寸
int CurrentSize; //当前已有数组元素个数public :
RecurveArray ( int MaxSize =10 ) :
ArraySize ( MaxSize ), Elements ( new int[MaxSize] ){ }~RecurveArray ( ) { delete [ ] Elements; }void InputArray(); //输入数组的内容int MaxKey ( int n ); //求最大值int Sum ( int n ); //求数组元素之和
float Average ( int n ); //求数组元素的平均值
};
void RecurveArray :: InputArray ( ){ //输入数组的内容
cout << "Input the number of Array: /n";
for ( int i = 0; i < ArraySize; i++ ) cin >> Elements[i];
}
int RecurveArray :: MaxKey ( int n ) { //递归求最大值
if ( n == 1 ) return Elements[0];
int temp = MaxKey ( n - 1 );
if ( Elements[n-1] > temp ) return Elements[n-1];
else return temp;
}
int RecurveArray :: Sum ( int n ) { //递归求数组之和
if ( n == 1) return Elements[0];
else return Elements[n-1] + Sum (n-1);
}
float RecurveArray :: Average ( int n ) { //递归求数组的平均值
if ( n == 1) return (float) Elements[0];
else return ( (float) Elements[n-1] + ( n - 1) * Average ( n - 1 ) ) / n;
}
int main ( int argc, char* argv [ ] ) { int size = -1;
cout << "No. of the Elements : ";
while ( size < 1 ) cin >> size;
RecurveArray ra ( size );
ra.InputArray();
cout<< "/nThe max is: " << ra.MaxKey ( ra.MaxSize ) << endl;
cout<< "/nThe sum is: " << ra.Sum ( ra.MaxSize ) << endl;
cout<< "/nthe avr is: " << ra.Average ( ra.MaxSize ) << endl;
return 0;
}
5-5 已知f为单链表的表头指针, 链表中存储的都是整型数据,试写出实现下列运算的递归算法:
(1) 求链表中的最大整数。
(2) 求链表的结点个数。
(3) 求所有整数的平均值。
【解答】
#include <iostream.h> //定义在头文件"RecurveList.h"中
class List;
class ListNode { //链表结点类
friend class List;
private:
int data; //结点数据
ListNode *link; //结点指针
ListNode ( const int item ) : data(item), link(NULL) { } //构造函数
};
class List { //链表类
private:
ListNode *first, current;
int Max ( ListNode *f );
int Num ( ListNode *f );
float Avg ( ListNode *f, int& n );
public:
List ( ) : first(NULL), current (NULL) { } //构造函数
~List ( ){ } //析构函数
ListNode* NewNode ( const int item ); //创建链表结点, 其值为item
void NewList ( const int retvalue ); //建立链表, 以输入retvalue结束
void PrintList ( ); //输出链表所有结点数据
int GetMax ( ) { return Max ( first ); } //求链表所有数据的最大值
int GetNum ( ) { return Num ( first ); } //求链表中数据个数
float GetAvg ( ) { return Avg ( first ); } //求链表所有数据的平均值
};
ListNode* List :: NewNode ( const int item ) { //创建新链表结点
ListNode *newnode = new ListNode (item);
return newnode;
}
void List :: NewList ( const int retvalue ) { //建立链表, 以输入retvalue结束
first = NULL; int value; ListNode *q;
cout << "Input your data:/n"; //提示
cin >> value; //输入
while ( value != retvalue ) { //输入有效
q
Recursive function相关推荐
- eg 6-20 A recursive function finds the greatest commen divisor of two integer
/*编写一个递归函数gcd,求解两个整数m和n的最大公约数 */int gcd(int m,int n) //gcd函数的定义,功能是递归求解m和n的最大公约数 {if(n==0) return m; ...
- POJ1579 HDU1331 HDU1579 ZOJ1168 Function Run Fun【记忆化递归】
Function Run Fun Description We all love recursion! Don't we? Consider a three-parameter recursive f ...
- POJ - 1579 Function Run Fun
Function Run Fun POJ - 1579 AYIT-2021 609暑假集训第一周下 记忆化搜索和背包 We all love recursion! Don't we? Consider ...
- fortran function的result
这个就是用来给返回值换一个名字. 比如: FUNCTION GetSomeData( someinput ) RESULT( ans )ans = f( someinput )end function ...
- 2018 Google kickstart Problem A. Planet Distance
题目描述 Small dataset 3 ≤ N ≤ 30. Large dataset 3 ≤ N ≤ 1000.input 2 5 1 2 2 3 3 4 2 4 5 3 3 1 2 3 2 1 ...
- 边界填充算法讲解_边界填充算法
边界填充算法讲解 Boundary fill is the algorithm used frequently in computer graphics to fill a desired color ...
- javascript计时器_JavaScript计时器:您需要了解的一切
javascript计时器 by Samer Buna 通过Samer Buna JavaScript计时器:您需要了解的一切 (JavaScript Timers: Everything you n ...
- javascript 堆栈_JavaScript调用堆栈-它是什么以及为什么它是必需的
javascript 堆栈 The JavaScript engine (which is found in a hosting environment like the browser), is a ...
- 在JavaScript中重复字符串的三种方法
In this article, I'll explain how to solve freeCodeCamp's "Repeat a string repeat a string" ...
最新文章
- 百练,4103,踩方格
- 一个丧心病狂的Github项目:东北话编程,大写的服!
- 一 Storm 基础
- 成功解决利用pandas的read_csv函数读取csv文件的时候出现中文乱码问题
- ACL 2019开源论文 | 基于图匹配神经网络的跨语言知识图对齐
- Qt Creator设置Nimble
- uva 11971 Polygon
- oracle 实现 自增主键功能
- URAL 1806 Mobile Telegraphs
- Linux 服务器为什么被黑
- Atitit 微服务实践 艾提拉著 微服务主要解决几个问题负载均很 目录 1. 微服务的模式 http请求层 vs服务层	1 1.1. Http vs 服务层优缺点	1 2. 实现技术	2
- MySQL数据库入门:建库、建表、插入数据
- css中实现子父元素高度自适应子元素高度
- Roaring64NavigableMap(Bitmap)简单使用
- 新概念,数字游民面临的一些挑战和应对举措
- 低照度水下图像增强论文(L2UWE)+代码+评价方法
- SDUTOJ 2777 小P的故事——神奇的换零钱 背包
- 一个微信小程序的案例
- Aquarius 水瓶
- 2、JavaScript快速入门
热门文章
- 2022第四届长安杯检材一wp
- python黑客库长安十二时辰 更新_爬取3万+评论,告诉你究竟是哪些人不喜欢《长安十二时辰》?...
- Mysql 时间与时区
- Int16, Int32, Int64 范围
- Gson的JsonParser使用
- python编程midi键盘按键_用树莓派自制 MIDI 键盘
- C语言串的基本操作(assign,compare,concat,index,insert,delete,replace)
- 《魔兽争霸3》战网命令详解
- MySQL UDF 提权
- 大数据Impala系列之初识Impala