/*
#include <iostream>
using namespace std;
void countdown(int n);
int main()
{
countdown(4);
return 0;
}
void countdown(int n)
{
cout << "Counting down ..." << n << endl;
if(n>0)
countdown(n-1);
cout << n << ": Kaboom!\n";
}
*/
/*
Counting down ...4
Counting down ...3
Counting down ...2
Counting down ...1
Counting down ...0
0: Kaboom!
1: Kaboom!
2: Kaboom!
3: Kaboom!
4: Kaboom!
Process returned 0 (0x0) execution time : 0.266 s
Press any key to continue.
*/
#include<iostream>
using namespace std;
const int Len = 66;
const int Divs = 6;
void subdivide(char ar[], int low, int high, int level);
int main()
{
char ruler[Len];
int i;
for(i=1; i<Len-2; i++)
ruler[i] = ' ';
ruler[Len-1] = '\0';
int max = Len-2;
int min = 0;
ruler[min] = ruler[max] = '|';
cout << ruler << endl;
for(i=1; i<=Divs; i++)
{
subdivide(ruler, min, max, i);
cout << ruler << endl;
for(int j=1;j<Len-2; j++)
ruler[j] = ' ';
}
return 0;
}
void subdivide(char ar[], int low, int high, int level)
{
if(level == 0)
return;
int mid = (high+low)/2;
ar[mid] = '|';
subdivide(ar, low, mid, level-1);
subdivide(ar, mid, high, level-1);
}
/*
| |
| | |
| | | | |
| | | | | | | | |
| | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Process returned 0 (0x0) execution time : 0.078 s
Press any key to continue.
*/
递归的两个例程
最新推荐文章于 2023-08-16 10:33:20 发布