用c++编写一个递归程序 使输入的5个字符反向输出

来源:百度知道 编辑:UC知道 时间:2024/05/17 03:00:57
用c++编写一个递归程序 使输入的5个字符反向输出

#include<iostream>
using namespace std ;
#define N 5
void f(int a[],int n);

int main ( )
{int a[N],n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
f(a,n) ;
return 0;}

void f(int a[N],int n)
{ cout<<a[n];
if(n>2) f(a,n-1);
else cout<<a[1];
}
运行结果
5
1 2 3 4 5
54321Press any key to continue

下面的C代码可以完成“用递归的方法将一个字符串逆序的结果打印出来”,已经过调试,希望对你有用。

#include <stdio.h>

void rev_str(char *p);

void main()
{
rev_str("I am a boy.");
}

void rev_str(char *p)
{
if ((p == (char *)NULL) || (*p == '\0'))
{
return;
}
else
{
rev_str(p + 1);
printf("%c", *p);
}
}

#include <iostream>
#include <conio.h>

void foo(int i) {
if(i <= 0) return;
else {