函数和递归的练习题

本文介绍了几个编程挑战,包括古代密码的加密方法,刽子手游戏的胜利条件判断,以及不同类型的数学问题,如求解最大质因子序列、递归计算特定函数值等。这些题目涉及字符串操作、递归算法和逻辑推理,旨在检验编程者的算法设计和问题解决能力。
摘要由CSDN通过智能技术生成

目录

A - 古老的密码

B - 刽子手游戏

C - 救济金发放

D - 最大质因子序列

E - 求 f ( x / n )

F - 再求 f ( x / n )

 G - M Function G

H - 闰年展示

求组合数(高效递归版)

显示菱形(递归版)

 寻找自守数(递归版)

互质数判断(递归)

hypot

assert

递归函数


A - 古老的密码

Ancient Roman empire had a strong government system with various departments, including a secret service department. Important documents were sent between provinces and the capital in encrypted form to prevent eavesdropping. The most popular ciphers in those times were so called substitution cipher and permutation cipher.

Substitution cipher changes all occurrences of each letter to some other letter. Substitutes for all letters must be different. For some letters substitute letter may coincide with the original letter. For example, applying substitution cipher that changes all letters from ‘A’ to ‘Y’ to the next ones in the alphabet, and changes ‘Z’ to ‘A’, to the message “VICTORIOUS” one gets the message “WJDUPSJPVT”. Permutation cipher applies some permutation to the letters of the message. For example, applying the permutation 〈2, 1, 5, 4, 3, 7, 6, 10, 9, 8〉 to the message “VICTORIOUS” one gets the message “IVOTCIRSUO”.

It was quickly noticed that being applied separately, both substitution cipher and permutation cipher were rather weak. But when being combined, they were strong enough for those times. Thus, the most important messages were first encrypted using substitution cipher, and then the result was encrypted using permutation cipher. Encrypting the message “VICTORIOUS” with the combination of the ciphers described above one gets the message “JWPUDJSTVP”.

Archeologists have recently found the message engraved on a stone plate. At the first glance it seemed completely meaningless, so it was suggested that the message was encrypted with some substitution and permutation ciphers. They have conjectured the possible text of the original message that was encrypted, and now they want to check their conjecture. They need a computer program to do it, so you have to write one.

Input

Input file contains several test cases. Each of them consists of two lines. The first line contains the message engraved on the plate. Before encrypting, all spaces and punctuation marks were removed, so the encrypted message contains only capital letters of the English alphabet. The second line contains the original message that is conjectured to be encrypted in the message on the first line. It also contains only capital letters of the English alphabet. The lengths of both lines of the input file are equal and do not exceed 100.

Output

For each test case, print one output line. Output ‘YES’ if the message on the first line of the input file could be the result of encrypting the message on the second line, or ‘NO’ in the other case.

Sample Input

JWPUDJSTVP

VICTORIOUS

MAMA

ROME

HAHA

HEHE

AAA

AAA

NEERCISTHEBEST

SECRETMESSAGES

Sample Output

YES

NO

YES

YES

NO 

题目大意 

 给出一个字符串,可进行任意变化字母处理(注意如果定义A->Z,那么之后遇到的A都要进行->Z的变换),或任意字母换位置,对比给出的另一字符串,看能否由原字符串操作后得出

思路 

操作字符串不改变数量。通过比较排序后的两个字符串的字母各个个数,即可判断

代码 

//古老的密码 
#include<iostream>
#include<algorithm> //包含排序函数 
using namespace std;
bool check(string& a, string& b) {
    int n1[26]={0},n2[26]={0};
    int l=a.length(); //a字符串长度 
    for(int i=0;i<l;i++){
    	n1[a[i]-'A']++;  
    	n2[b[i]-'A']++; //各字母个数 
	}
	sort(n1,n1+26);
	sort(n2,n2+26);  //排序
	for(int i=0;i<26;i++){
		if(n1[i]!=n2[i]) return 0;  //如果有不同 一定不能得出
	}
	return 1;
}
int main()
{
	string s1, s2;
    while (cin >> s1 >> s2) {
    if (check(s1, s2))printf("YES\n");
    else printf("NO\n");
    }
    return 0;
	
}

B - 刽子手游戏

In “Hangman Judge,” you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follows:

1. The contestant tries to solve to puzzle by guessing one letter at a time.

2. Every time a guess is correct, all the characters in the word that match the guess will be “turned over.” For example, if your guess is ‘o’ and the word is “book”, then both ‘o’s in the solution will be counted as “solved”.

3. Every time a wrong guess is made, a stroke will be added to the drawing of a hangman, which needs 7 strokes to complete. Each unique wrong guess only counts against the contestant once. ______

|       |

|      O

|     / | \

|       |

|     /  \ 

|___

|     |______

 |_________|

4. If the drawing of the hangman is completed before the contestant has successfully guessed all the characters of the word, the contestant loses.

5. If the contestant has guessed all the characters of the word before the drawing is complete, the contestant wins the game.

6. If the contestant does not guess enough letters to either win or lose, the contestant chickens out. Your task as the “Hangman Judge” is to determine, for each game, whether the contestant wins, loses, or fails to finish a game.

Input

Your program will be given a series of inputs regarding the status of a game. All input will be in lower case. The first line of each section will contain a number to indicate which round of the game is being played; the next line will be the solution to the puzzle; the last line is a sequence of the guesses made by the contestant. A round number of ‘-1’ would indicate the end of all games (and input).

Output

The output of your program is to indicate which round of the game the contestant is currently playing as well as the result of the game. There are three possible results: You win. You lose. You chickened out.

Sample Input

1

cheese

chese

2 cheese

abcdefg

3 cheese

abcdefgij

-1

Sample Output

Round 1

You win.

Round 2

You chickened out.

Round 3

You lose.

题目大意 

给定字符串,每次猜一个字母,猜中记一次,猜错记一次,猜错满七次 "You chickened out." 不满七次且猜中全部字母 "You win. ",其他情况 " You lose. "

代码 

//古老的密码 
#include<iostream>
using namespace std;
int check(string& a, string& b) {
    int nogugess = 0;//w未被猜出字符数 
    int wrong = 0;//猜错次数
    int n1[26] = { 0 }, n2[26] = { 0 };
    for (int i = 0; i < a.length(); i++)n1[a[i] - 'a']++;//标记字符
    for (int i = 0; i < 26; i++)if (n1[i])nogugess++;  //记录有几种字母
    for (int i = 0; i < b.length(); i++) {//遍历b
         if (!n2[b[i] - 'a']) {
             if (n1[b[i] - 'a']) {
                 nogugess--; //猜中 
                 if (nogugess == 0)return 0; //全猜中
                }
        else if (!n1[b[i] - 'a']) {//猜错 
                 wrong++;
                 if (wrong >= 7)return 1;  //猜错7次
        }
    }
    n2[b[i] - 'a'] = 1; //猜过的字母
    }
    return 2;
}
int main()
{
    int n;
    string s1, s2;
    string str[] = { "You win.","You lose.","You chickened out."};//三种情况 
    while(~scanf("%d", &n) && n != -1) {
           printf("Round %d\n", n);
           cin >> s1 >> s2;
           cout << str[check(s1, s2)]<<endl; 
    }
    return 0;
}

C - 救济金发放

In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is arbitrarily chosen as number 1, and the rest are numbered counterclockwise up to N (who will be standing on 1’s left). Starting from 1 and moving counter-clockwise, one labour official counts off k applicants, while another official starts from N and moves clockwise, counting m applicants. The two who are chosen are then sent off for retraining; if both officials pick the same person she (he) is sent off to become a politician. Each official then starts counting again at the next available person and the process continues until no-one is left. Note that the two victims (sorry, trainees) leave the ring simultaneously, so it is possible for one official to count a person already selected by the other official.

Input

Write a program that will successively read in (in that order) the three numbers (N, k and m; k, m > 0, 0 < N < 20) and determine the order in which the applicants are sent off for retraining. Each set of three numbers will be on a separate line and the end of data will be signalled by three zeroes (0 0 0).

Output

For each triplet, output a single line of numbers specifying the order in which people are chosen. Each number should be in a field of 3 characters. For pairs of numbers list the person chosen by the counterclockwise official first. Separate successive pairs (or singletons) by commas (but there should not be a trailing comma). Note: The symbol ⊔ in the Sample Output below represents a space.

Sample Input

10 4 3 0 0 0

Sample Output

␣␣4␣␣8,␣␣9␣␣5,␣␣3␣␣1,␣␣2␣␣6,␣10,␣␣7 

题目大意 

N个人围成圈,一人从1->N数到k, k出圈,输出编号k,,一人从N->1数到m, m出圈,输出编号m(若k==m输出一次)

代码 

//救济金发放 
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
bool cir[maxn];
int r,l,Len;

int _find(int pos,int len,int dir)
//dir代表方向,1代表向右,0代表向左,pos是当前位置,len代表要走多少步
{
    while(len)
    {
        if(dir == 1)  pos++;
        else          pos--;
        if(pos > Len) pos = 1;
        if(pos < 1)   pos = Len;
        if(!cir[pos]) len--; //有人出圈长度减小
    }
    return pos;
}

int main()
{
    int len,m,k;
    while(scanf("%d%d%d",&len,&m,&k) && len+m+k) 
    {
        Len = len;
        memset(cir,false,sizeof(cir));//清零
        bool flag = false;
        r = 0,l = Len+1;
        while(len)
        {
            r = _find(r,m,1);
            l = _find(l,k,0);
            cir[r] = cir[l] = true;//出圈的人 
            if(r == l)
            {
                if(!flag)  //控制,的输出
                    printf("%3d",r);
                else
                    printf(",%3d",r);
                len--;
            }
            else
            {
                if(!flag)
                    printf("%3d%3d",r,l);
                else
                    printf(",%3d%3d",r,l);
                len -= 2;
            }
            flag = true;
        }
        printf("\n");
    }
    return 0;
}

D - 最大质因子序列

Description

任意输入两个正整数m,n(2≤m≤n≤5000),依次输出 m 到 n 之间每个数的最大质因子(包括 m 和 n;如果某个数本身是质数,则输出这个数自身)。

Input

一行,包含两个正整数 m 和 n,其间以单个空格间隔。

Output

一行,每个整数的最大质因子,以逗号间隔。

Sample 1

InputcopyOutputcopy
5 10
5,3,7,2,3,5

代码 

#include <cstdio>
int f1(int m)//判断是否为质数 
{
	for(int j=2;j*j<=m;j++){
		if(m%j==0){
			return 0;
		}  
	}
	return 1;
}
int f2(int m)//找最大质因子 
{
	if(f1(m)==1) return m; //m为质数则输出m 
	for(int i=m/2;i>=2;i--){
		if(m%i==0&&f1(i)==1){
			return i;
		}
	}
}
void f3(int m,int n)//遍历m到n 
{
	if(m==n){
		printf("%d",f2(m));
		return;
	}
	printf("%d,",f2(m));
	f3(m+1,n);
}
int main()
{
	int m,n;
	scanf("%d%d",&m,&n);
	f3(m,n);
	return 0;
} 

E - 求 f ( x / n )

Description

已知  f ( x / n ) = sqrt ( n + sqrt ( (n−1) + sqrt ( (n−2) +...+ sqrt ( 2 + sqrt ( 1 + x​​​​​ ) ) ) ) )

计算 f 的值。

Input

输入 x 和 n。

Output

函数值,保留两位小数。

Sample 1

InputcopyOutputcopy
4.2 10
3.68

 代码

//求 f(x,n)
#include <cstdio>
#include<cmath>
double f(double x,long long n)
{
	if(n==1) return sqrt(1+x);
	return sqrt(n+f(x,n-1));
}
int main()
{
	double x;
	long long n;
	scanf("%lf%lld",&x,&n);
	printf("%.2lf",f(x,n));
	return 0;
} 

F - 再求 f ( x / n )

题目描述

         已知 f ( x / n ) =         \frac{x}{n+\frac{x}{n-1+\frac{x}{n-2+\frac{...}{...+\frac{x}{1+x}}}}}

                                    

            

用递归函数求解。

Input

第一个数是 x 的值,第二个数是 n 的值。(n 为整数)

Output

函数值,保留两位小数。

Sample 1

InputcopyOutputcopy
1
2
0.40

代码

//再求 f(x,n)
#include <cstdio>
double f(double x,int n)
{
	if(n==1) return x/(1+x);
	return x/(n+f(x,n-1));
}
int main()
{
	double x;
	int n;
	scanf("%lf%d",&x,&n);
	printf("%.2lf",f(x,n));
	return 0;
}

 G - M Function G

Description对于一个长度为 n 的正整数数列 a

Input

输入共两行。 第一行为一个整数 n。 第二行为 n 个整数 a1​,a2​,⋯,an​,对应题目中的正整数数列 a。

Output

输出共一行一个整数,代表 M(1,n) 的值。

Sample 1

InputcopyOutputcopy
10
3 72 26 91 5 84 18 29 50 23
11

Hint

样例 1 解释 我们这里暂时使用 max{al​,al+1​,⋯,ar​} 来表示 al​,al+1​,⋯,ar​ 中的最大值。M(1,10)=M(1,5)modmax⁡(M(6,10),7)(a5−1)

            =max⁡{a1,a2⋯,a5}modmax⁡(max⁡{a6,a7⋯,a10},7+(a5−1)

            =max⁡{a1,a2⋯,a5}modmax⁡(84,7)+(a5−1)

            =max⁡{a1,a2⋯,a5}mod84+(a5−1)

            =91mod84+(a5−1)

            =7+(a5−1)=1

数据规模与约定 \

对于 100%100% 的数据,保证 1≤n≤5×10^5,1≤ai​≤10^9。

 代码

//M Function G
#include <bits/stdc++.h>
using namespace std;
int n;
long long a[500005];
long long M(int l, int r) {

  if(abs(r-l)<=5){
     long long ret=0;
  for(int i=l;i<=r;++i)
      ret=max(ret,a[i]);
     return ret;    //情况1:r-l<=5
  }
  else{
  int mid=(l+r)/2;
  return M(l,mid)%max(M(mid+1,r),7ll)+a[mid]-1;
}         //情况2:r-l>5      //这里7转为long long   
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		cin>>a[i];
	cout<<M(1,n)<<endl;
	return 0;
}

 

H - 闰年展示

Description

输入 x,y,输出 [x,y] 区间中闰年个数,并在下一行输出所有闰年年份数字,使用空格隔开。

Input

输入两个正整数x,y,以空格隔开。

Output

第一行输出一个正整数,表示[x,y] 区间中闰年个数。

第二行输出若干个正整数,按照年份单调递增的顺序输出所有闰年年份数字。

Sample 1

InputcopyOutputcopy
1989 2001
3
1992 1996 2000

Hint

数据保证,1582≤x<y≤3000。

代码 

#include <bits/stdc++.h>
using namespace std;
int num=0; 
int a[1500];
int is_rn(int x) //判断是否为闰年 
{
	if((x%400==0)||(x%4==0&&x%100!=0)) return 1;
	return 0;
}
void find_rn(int x,int y)
{
	if(x>y) return;
	if(is_rn(x))
	   a[num++]=x; //记录每个闰年 
	find_rn(x+1,y);
}
int main()
{
	int x,y;
	scanf("%d%d",&x,&y);
	find_rn(x,y);
	printf("%d\n",num);
	for(int i=0;i<num;i++){
		printf("%d ",a[i]);
	}
	printf("\n");
	
	return 0;
}

求组合数(高效递归版)

函数原型

double Cmb(int x, int y); 

说明:x 和 y 为非负整数,且 x≥y,函数值为组合数 C_{x}^{y} 

裁判程序 

#include <stdio.h>

double Cmb(int x, int y);

int main()
{
    int m, n;
    scanf("%d%d", &m, &n);
    printf("%.10g\n", Cmb(m, n));
    return 0;
}

/* 你提交的代码将被嵌在这里 */

 

要求:不要使用循环语句,不调用阶乘函数和排列数函数。找出递推公式,该函数直接调用自己求得结果。 

输入样例

4 2

输出样例

6

输入样例2

34 17

输出样例2

2333606220

 代码

double Cmb(int x, int y) {
    y = y < (x - y) ? y : x - y;//保证y取值为较小的那部分
    if (y == 0)return 1;
    return x * Cmb(x - 1, y - 1) / y;//递推
}

 

显示菱形(递归版)

请编写函数,显示菱形。

函数原型

void Diamond(int height, char symbol);

说明:参数 height 为菱形的高,symbol 为显示字符。函数将在屏幕上显示高度和底宽为 height 由字符 symbol 组成的菱形。若 height 为偶数,或者小于等于 0,则不输出。

提示:需要利用前面作业中的 Show 函数、IsOdd 函数或 IsEven 函数,此外需要增加自用的内部函数。

裁判程序

#include <stdio.h>

int IsOdd(int number);
int IsEven(int number);
void Show(int number, char symbol);
void Diamond(int height, char symbol);

int main()
{
    int n;
    char s;
    scanf("%d %c", &n, &s);
    Diamond(n, s);
    return 0;
}

......

/* 你提交的代码将被嵌在这里 */

输入样例1

-3 #

输出样例1

注:无输出。

输入样例2

5 @

输出样例2

  @
 @@@
@@@@@
 @@@
  @

要求:不得使用循环语句。

 代码

void f(int pos, int height, char symbol, int d)
{
	if(height==0) return ;
	if(d>0){ //打印上三角
	   f(pos+1,height-1,symbol,d); //先递归再打印 
	   Show(pos,' '); Show(2*height-1,symbol); Show(1,'\n');
	}
	else{ //打印下三角
	   Show(pos,' '); Show(2*height-1,symbol); Show(1,'\n');
	   f(pos+1,height-1,symbol,d);  //先打印再递归 
	}
}
void Diamond(int height, char symbol)
{
	int n=1;
	if(IsEven(height)||height<=0)  return ;
	f(1,height/2,symbol,1); //上三角 
	Show(height,symbol); Show(1,'\n'); //中间 
	f(1,height/2,symbol,-1);  //下三角 
}

 寻找自守数(递归版)

所谓自守数(也称守形数),是指其平方数的低位部分恰为该数本身的自然数。例如:252=625, 因此 25 是自守数。其中:0 和 1 也是自守数。

请编写函数,输出指定范围内的所有自守数。

函数原型

void FindAutomorphic(long long lower, long long upper);

 

说明:参数 lower 和 upper 分别为整数区间的下限和上限。若在该范围内存在自守数,则输出这些自守数,否则输出“None”。

裁判程序

#include <stdio.h>

#define Sqr(x) ...(略)...

int IsLowerPart(long long x, long long y);
int IsAutomorphic(long long x);
void FindAutomorphic(long long lower, long long upper);

int main()
{
    long long a, b;
    scanf("%lld%lld", &a, &b);
    FindAutomorphic(a, b);
    return 0;
}

...(略)...

/* 你提交的代码将被嵌在这里 */

 

输入样例1

10 80

输出样例1

25
76

输入样例2

400 600

输出样例2

None

 代码

int num;
void fun(long long lower, long long upper)
{
    if(lower>upper) return ;//出界终止
    if(IsAutomorphic(lower))//判断为自守数
        printf("%d\n",lower),num++;
    fun(lower+1,upper);
}
void FindAutomorphic(long long lower, long long upper)
{
    num=0;//每次调用要初始化
    fun(lower,upper);
    if(num==0)puts("None");//无自守数
    
}

互质数判断(递归)

要求实现一个递归函数,能够高效判断两个正整数a,b(0<a,b<10^9)是否为互质数(最大公约数为1)。

函数接口定义:

bool check(int a, int b);

其中 a 、b是用户传入的参数,存放待判断是否为互质数的两个正整数。

裁判测试程序样例:

#include<iostream>
using namespace std;

//输入n对整数,统计其中互质数的个数,处理到文件尾 
int main() {
    int n;
    while(cin>>n) {
        int cnt=0;
        for(int i=0; i<n; i++) {
            int a,b;
            cin>>a>>b;
            if(check(a,b)==true) cnt++;
        }
        cout<<cnt<<endl;
    }
    return 0;
}

输入样例:

3
3 11
5 11
10 12

输出样例:

2

代码 

int gcd(int a,int b)
{
    if(a<=0||b<=0) return 0;
    if(a%b==0) return b; //求最大公约数
    else gcd(b,a%b); 
}
bool check(int a, int b)
{
    if(gcd(a,b)==1) //最大公约数为1则互质
        return true;
    return false;
}

 

hypot

(1)hypot函数的功能是计算一直角三角形的斜边长度。
(2)函数hypot(x,y)表示根据直角三角形的两直角边长度x和y计算其斜边的长度。
或者是从标点(x,y)到原点的距离,该函数的算法等同于sqrt(x*x+y*y)。 

hypot函数头文件math.h或cmath
hypot(a,b)的返回值为double类型

assert

(1)断言(assert)的语义如下:如果表达式的值为0(假),则输出错误消息并终止程序的执行
(一般还会出对话框,说明在什么地方引发了assert);如果表达式为真,则不进行任何操作。因
此,断言失败就表明程序存在一个bug。
(2)C/C++的宏(assert)就是这样的断言,当表达式为假时,调用库函数abort()终止程序。
(3)程序中可以把assert看成一个在任何系统状态下都可以安全使用的无害测试手段,所以不要把
程序中的assert语句删除掉。
(4)如果程序在assert处终止了,并不是说含有该assert的函数有错误,而是调用函数出了差错,
assert可以帮助我们追踪到错误发生的原因。
(5)在函数的入口处,建议使用断言来检查参数的有效性(合法性)。请给assert语句加注释,告
诉人们assert语句究竟要干什么。
编程时合理利用assert宏,将给调试带来很大的方便。
总而言之,在实际的系统中,“一个地方的参数错误就引起整个程序异常退出”是不可取的,在编
写和调试算法程序中,assert会“迫使”编写出更高质量的程序。

递归函数

递归函数必须满足两个条件:
(1)必须有一个终止准则(递归的边界条件、递归的结束条件);
(2)在每一次调用自己时,必须是(在某种意义上)更接近于解
(递推公式或递归方程);
边界条件与递归方程是递归函数的二个要素。若没有条件(1),则递归无
从终止;若没有条件(2),则不是递归。

递归调用过程(两个阶段)
(1)递推阶段
将原问题不断地分解为新的子问题,逐渐从未知的向已知的方向推进,
最终达到已知的条件,即递归结束条件,这时递推阶段结束。
(2)回归阶段
从已知条件出发,按照“递推”的逆过程,逐一求值回归,最终到达
“递推”的开始处,结束回归阶段,完成递归调用。 

 

好的,以下是一些 C++ 中的递归函数练习题: 1. 编写一个递归函数,计算 n 的阶乘 ```cpp int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } } ``` 2. 编写一个递归函数,计算斐波那契数列的第 n 项 ```cpp int fibonacci(int n) { if (n == 0 || n == 1) { return n; } else { return fibonacci(n - 1) + fibonacci(n - 2); } } ``` 3. 编写一个递归函数,计算一个数字的二进制表示中 1 的个数 ```cpp int countOnes(int n) { if (n == 0) { return 0; } else { return (n % 2 == 1) + countOnes(n / 2); } } ``` 4. 编写一个递归函数,计算一个数字的反转 ```cpp int reverse(int n) { if (n < 10) { return n; } else { return (n % 10) * pow(10, floor(log10(n))) + reverse(n / 10); } } ``` 5. 编写一个递归函数,计算一个字符串的长度 ```cpp int stringLength(string str) { if (str == "") { return 0; } else { return 1 + stringLength(str.substr(1)); } } ``` 6. 编写一个递归函数,判断一个字符串是否是回文字符串 ```cpp bool isPalindrome(string str) { if (str.length() <= 1) { return true; } else if (str[0] != str[str.length() - 1]) { return false; } else { return isPalindrome(str.substr(1, str.length() - 2)); } } ``` 7. 编写一个递归函数,计算一个数组中所有元素的和 ```cpp int arraySum(int arr[], int size) { if (size == 0) { return 0; } else { return arr[0] + arraySum(arr + 1, size - 1); } } ``` 8. 编写一个递归函数,计算一个数组中所有元素的平均值 ```cpp double arrayAverage(int arr[], int size) { if (size == 0) { return 0; } else { return (arr[0] + (size - 1) * arrayAverage(arr + 1, size - 1)) / size; } } ``` 9. 编写一个递归函数,计算一个数组中的最大值 ```cpp int arrayMax(int arr[], int size) { if (size == 1) { return arr[0]; } else { return max(arr[0], arrayMax(arr + 1, size - 1)); } } ``` 10. 编写一个递归函数,计算一个数组中的最小值 ```cpp int arrayMin(int arr[], int size) { if (size == 1) { return arr[0]; } else { return min(arr[0], arrayMin(arr + 1, size - 1)); } } ``` 希望这些练习题能够帮助你练习 C++ 中的递归函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值