C++ 如何在c+中使用*打印X形状+;不使用任何循环

C++ 如何在c+中使用*打印X形状+;不使用任何循环,c++,recursion,ascii-art,C++,Recursion,Ascii Art,我想使用n行数打印一个X形状,但没有任何循环。我必须使用递归函数来打印X 我已经调用了函数和空间函数,但它没有打印X。它打印了: * * * * * * 我必须用递归来解决这个问题。不允许对循环使用,也不允许对循环使用来解决此问题 // C++ implementation to print the given // pattern recursively #include <bits/stdc++.h> using namespace std; // functi

我想使用
n
行数打印一个X形状,但没有任何循环。我必须使用递归函数来打印X

我已经调用了函数和空间函数,但它没有打印X。它打印了:

*   *
 *  *
  * *
我必须用递归来解决这个问题。不允许对循环使用
,也不允许对
循环使用
来解决此问题

// C++ implementation to print the given
// pattern recursively
#include <bits/stdc++.h>

using namespace std;

// function to print the 'n-th' row of the
// pattern recursively
int g;

void printPatternRowRecur(int n) {
    if (n < 1)
        return;

    if (n = 1) {
        cout << "*";
    }
    // print the remnaining stars of the n-th row
    // recursively
    else {
        return;
    }
    printPatternRowRecur(n - 1);
}

void print_space(int space) {
    // base case
    if (space == 0)
        return;
    cout << " ";

    // recursively calling print_space()
    print_space(space - 1);
}

int s;

void Rhombus(int n) {
    // base condition
    if (s >= n)
        return;
    else {
       print_space(s);
       printPatternRowRecur(n);
       print_space(n - s);
       printPatternRowRecur(n);
    }
    // print the stars of the n-th row
    s++;
    // move to next line
    cout << endl;
    // print stars of the remaining rows recursively
    Rhombus(n);
}

 // Driver program to test above
int main() {
    int n = 3;
    //cout << "Enter the number of lines you want to print" << endl;
    //cin >> n;
    //cout << endl << "Rhombus" << endl;
    Rhombus(n);
    return 0;
}
<代码> //C++实现打印给定 //递归模式 #包括 使用名称空间std; //函数用于打印文件的第n行 //递归模式 int g; void printPatternRowRecur(int n){ if(n<1) 返回; 如果(n=1){ cout
if(n=1)
应该是
if(n==1)
。编码时,
if
表达式始终为真

以下是一个简化版本:

// C++ implementation to print the given pattern recursively
#include <iostream>

using namespace std;

void print_spaces(int n) {
    if (n > 0) {
        cout << ' ';
        print_spaces(n - 1);
    }
}

void rhombus(int s, int n) {
    if (s < n) {
        int left = min(s, n - s - 1);
        int middle = n - 2 * left - 2;
        print_spaces(left);
        cout << '*';
        if (middle >= 0) {
            print_spaces(middle);
            cout << '*';
        }
        cout << endl;
        // print stars of the remaining rows recursively
        rhombus(s + 1, n);
    }
}

void rhombi(int s, int n) {
    if (s <= n) {
        cout << endl << "Rhombus " << s << endl;
        rhombus(0, s);
        rhombi(s + 1, n);
    }
}

int main() {
    //int n = 3;
    //cout << "Enter the number of lines you want to print" << endl;
    //cin >> n;
    //cout << endl << "Rhombus" << endl;
    //rhombus(0, n);
    rhombi(0, 7);
    return 0;
} 
<代码> //C++实现递归打印给定模式 #包括 使用名称空间std; 无效打印空间(整数n){ 如果(n>0){
cout我将使用两个递归函数:第一个是在若干空格后写一个字符,第二个是写行

#include <iostream>

/*************************************
 * display on character (c) on out after x spaces recursively
 * **********************************/
void display_char(int x, std::ostream& out = std::cout, char c='*') {
    if (x == 0) out << c;
    else {
        out << ' ';
        display_char(x-1, out, c);
    }
}

/*********************************
 * displays lines to draw an X pattern
 * The pattern is composed of c characters on out
 * x decreases to 0 while y increases and a c 
 * is printed at positions x and y
 * ******************************/
void display_line(int x, int y=0, std::ostream& out=std::cout, char c='*') {
    int oldx=x, oldy=y;
    if (x < y) {
        int t = x;
        x = y;
        y = t;
    }
    display_char(y, out, c);
    if (x != y) display_char(x - y, out, c);
    out << '\n';
    if (oldx > 0) display_line(oldx-1, oldy+1, out, c);
}

int main() {
    int x;
    std::cout << "X size (int): ";
    std::cin >> x;
    display_line(x-1);
    return 0;
}
#包括
/*************************************
*在x空格后递归显示字符(c)on out
* **********************************/
void display_char(int x,std::ostream&out=std::cout,char c='*')){
如果(x==0)out
#包括
使用名称空间std;
// *   *
//  * *
//   *
//  * *
// *   *
//PrintStarHorizontal将使用x坐标在一行上递归打印“*”
水平方向无效打印开始(int-xcoordinate 1、int-xcoordinate 2、int-currentXCoordinate、int-maxCoordinate){
如果(currentXCoordinate>=maxCoordinate){

cout这是一个递归,包含一个函数、一个给定参数和两个默认参数:

#包括
使用名称空间std;
空f(整数y,整数x=1,整数w=0){
if(y<1)
返回;
如果(x>0){
f(y,-(y+2*w),w);
f(y-2,x,w+1);
如果(y>1)
f(y,-(y+2*w),w);
返回;
}
如果(x==-w){
库特
#include <iostream>
using namespace std;

// *   *
//  * *
//   *
//  * *
// *   *

//printStarHorizontally will recursively print '*' on a line using x coordinates
void printStarHorizontally(int xCoordinate1, int xCoordinate2, int currentXCoordinate, int maxCoordinate){
    if(currentXCoordinate >= maxCoordinate) {
        cout<<endl;
        return;
    }
    
    if(currentXCoordinate == xCoordinate1 || currentXCoordinate == xCoordinate2) {
        cout<<"*";
    } else {
        cout<<" ";
    }
    printStarHorizontally(xCoordinate1, xCoordinate2, currentXCoordinate + 1, maxCoordinate);
}

//PrintCross will go to each height and then use printStarHorizontally func. to print star on that particular height
void PrintCross(int heightOfCross, int currentHeight) {
    if(currentHeight >= heightOfCross) return;
    
    printStarHorizontally(currentHeight, heightOfCross-currentHeight-1, 0, heightOfCross);
    
    PrintCross(heightOfCross,currentHeight+1);
}

int main() {
    // heightOfCross should be odd integer
    int heightOfCross = 13;
    PrintCross(heightOfCross, 0);
    return 0;
}