ep3_15.cpp

来自「这里有大量的c语言习题呢!真的是题海哦」· C++ 代码 · 共 51 行

CPP
51
字号
//3.15 下面递归函数执行结果是什么?
#include<iostream>
using namespace std;

void p1(int w){
	int i;
	if(w>0){
		for(i=0;i<w;i++) cout<<'\t'<<w;
		cout<<endl;
		p1(w-1);
	}
}

void p2(int w){
	int i;
	if(w>0){
		p2(w-1);
		for(i=0;i<w;i++) cout<<'\t'<<w;
		cout<<endl;
		p2(w-1);
	}
}

void p3(int w){
	int i;
	if(w>0){
		for(i=0;i<w;i++) cout<<'\t'<<w;
		cout<<endl;
		p3(w-1);
		p3(w-2);
	}
}

void p4(int w){
	int i;
	if(w>0){
		for(i=0;i<w;i++) cout<<'\t'<<w;
		cout<<endl;
		p4(w-1);
		for(i=0;i<w;i++) cout<<'\t'<<w;
		cout<<endl;
	}
}

int main(){
	p1(4);cout<<endl;
	p2(4);cout<<endl;
	p3(4);cout<<endl;
	p4(4);cout<<endl;
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?