如何在C++中使用*打印X形图案,而不使用任何循环?

3
我想使用 n 行打印出一个 X 形状,但不能使用任何循环语句。我必须使用递归函数来打印 X。
我已经调用了一些函数和空格函数,但它没有打印出 X,而是打印出:
*   *
 *  *
  * *

我必须使用递归解决这个问题。不允许使用for循环或while循环解决此问题。

// 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;
}
4个回答

3

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;
} 

1
这是一个具有一个函数、一个给定参数和两个默认参数的递归示例:
#include <iostream>
using namespace std;

void f(int y, int x=1, int w=0){
  if (y < 1)
    return;

  if (x > 0){
    f(y, -(y + 2 * w), w);
    f(y - 2, x, w + 1);
    if (y > 1)
      f(y, -(y + 2 * w), w);
    return;
  }

  if (x == -w){
    cout << endl;
    return;
  }

  if (x == -(w + y) || x == -(w + 1))
    cout << '*';
  else
    cout << ' ';

  f(y, x + 1, w);
}

int main(){
  f(5);

  cout << endl;

  f(6);

  return 0;
}

1
#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;
}

0
我会使用两个递归函数:第一个函数在一定数量的空格后写入字符,第二个函数用于写入行。
#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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接