C++ 打印雷电般的图案

C++ 打印雷电般的图案,c++,C++,我最近在一次采访中遇到了这个问题,当时我不得不打印一个模式。这个模式看起来微不足道,但我无法找到解决方案。请帮我识别代码中的错误 以下是测试用例: n = 1 x n = 2 x x x x n = 3 x x x o x x x n = 5 x x o x o x x o x o x x o x o x x n = 10 x x o x

我最近在一次采访中遇到了这个问题,当时我不得不打印一个模式。这个模式看起来微不足道,但我无法找到解决方案。请帮我识别代码中的错误

以下是测试用例:

n = 1
x


n = 2
 x
x x
 x


n = 3
  x
   x
x o x
 x
  x


n = 5
    x
     x
    o x
     o x
x o x o x
 x o
  x o
   x
    x


n = 10
         x
          x
         o x
          o x
         x o x
          x o x
         o x o x
          o x o x
         x o x o x
x o x o x x o x o x
 x o x o x
  x o x o
   x o x o
    x o x
     x o x
      x o
       x o
        x
         x
以下是我的尝试:

#include <iostream>
using namespace std;

string reverse(string s) {
  int start = 0, end = s.size()-1;
  while(start<end) {
    char temp = s[start];
    s[start] = s[end];
    s[end] = temp;
    ++start;
    --end;
  }
  return s;
}

int main() {
  int n;
  cin>>n;
  int count=0;
  string s = "X";
  bool wasLastX = true;
  for (int i=1;i<=n-1;++i) {
    for(int j=1;j<=n-1;++j) {
      cout<<" ";
    }
    cout<<s<<endl;
    if (i == n-1) {
      break;
    }
    if (s[0] == 'X' || s[0] == 'O') {
      s.insert(s.begin(), ' ');
    }
    else {
      if (wasLastX) {
        s.insert(s.begin(), 'O');
        wasLastX = false;
      }
      else {
        s.insert(s.begin(), 'X');
        wasLastX = true;
      }
    }
    ++count;
  }

  string temp = s;
  if (n%2 == 0) {
    cout<<s<<" "<<s<<endl;
  }
  else {
    if (s[1] == 'X') {
      s.insert(s.begin(), 'O');
    }
    else if (s[1] == 'O') {
      s.insert(s.begin(), 'X');
    }
    cout<<reverse(temp)<<s<<endl;
  }

  int count1 = 0;
  for (int i=1;i<=n-1;++i) {
    cout<<" ";
    for(int j=1;j<=count1;++j) {
      cout<<" ";
    }
    cout<<temp<<endl;
    temp.pop_back();
    ++count1;
  }
  return 0;
}
但是,对于n=2和n=10,代码工作正常。但是,对于n=4,它显示了错误的输出。所以,我不能概括它不适用于n的奇数值。 对于n=4,我得到了这个结果,这是错误的,因为中行后面的所有元素都应该以x开头,而我的元素则以o开头

   X
    X
   O X
O X O X
 O X
  O
   O

因为最初在这个问题上附加了一个
[java]
标记,所以我尝试用java代码实现它。希望这有助于为c代码构建逻辑

public class ThunderBoltPattern {
public static void main(String[] args) {
    int input = 5;
    int evenOddFactor = (input % 2 == 0) ? 0 : 1; // If it is even, add 1 else it should be 0
    // length on pattern based on input
    int length = input * 2 - evenOddFactor;

    // Using character array to store individual character at various point
    char[][] array = new char[length][length];
    int factor = 0;

    while (factor < input) {
        for (int i = 0 + factor, j = input - 1; i < input; i++, j++) {
            array[i][j] = 'x';
            array[j][i] = 'x';
        }
        for (int i = 2 + factor, j = input - 1; i < input; i++, j++) {
            array[i][j] = 'o';
            array[j][i] = 'o';
        }
         // Because character are repeated on same line after 4 digit, using 4 as factor
        factor += 4;
    }

    for (char[] chs : array) {
        for (char ch : chs) {
            System.out.print(ch);
        }
        System.out.println();
    }
}
}
公共类雷电模式{
公共静态void main(字符串[]args){
int输入=5;
int evenOddFactor=(输入%2==0)?0:1;//如果是偶数,则添加1,否则应为0
//基于输入的模式长度
int length=输入*2-偶数因子;
//使用字符数组在不同点存储单个字符
char[][]数组=新的char[length][length];
整数因子=0;
while(系数<输入){
对于(int i=0+因子,j=input-1;i
对不起,我无法遵循您为解决问题而开发的复杂逻辑。这里有一个非常简单的代码可以解决您的问题

#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

/* its important the next string is symmetrical */
string s1 = "x o x";
string s2 = "     ";

int main()
{
    int n, i, l = s1.size();

    cin >> n;
    while (n > l) {
        s1 += s1.substr(1, l-1);
        s2 += s2.substr(1, l-1);
        l = 2*l - 1;
    }

    for (i = 1; i < n; ++i) {
        cout << s2.substr(0, n-1)
             << s1.substr(l - i, l)
             << endl;
    }
    cout << s1.substr(0, n - 1)
         << s1.substr(l - n, n)
         << endl;
    for (i = 1; i < n; ++i) {
        cout << s2.substr(0, i)
             << s1.substr(0, n - i)
             << endl;
    }
}
#包括
#包括
#包括
使用名称空间std;
/*重要的是,下一根弦是对称的*/
字符串s1=“x o x”;
字符串s2=“”;
int main()
{
int n,i,l=s1.size();
cin>>n;
而(n>l){
s1+=s1.substr(1,l-1);
s2+=s2.substr(1,l-1);
l=2*l-1;
}
对于(i=1;i请展示一下你的尝试好吗/code@GBlodgett,请检查我的尝试。谢谢@GhostCat提供的建设性反馈。我的问题现在看起来好多了。从现在开始,我会记住这些要点。不客气。与真正倾听的人一起工作,然后采取相应行动总是很好的。问题现在重新开始。谢谢你的回答这个变化像一个魅力一样。非常简单,很容易理解。谢谢你!@Abhinav,请参阅我的编辑,了解如何简单地让它处理更大的值
n
#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

/* its important the next string is symmetrical */
string s1 = "x o x";
string s2 = "     ";

int main()
{
    int n, i, l = s1.size();

    cin >> n;
    while (n > l) {
        s1 += s1.substr(1, l-1);
        s2 += s2.substr(1, l-1);
        l = 2*l - 1;
    }

    for (i = 1; i < n; ++i) {
        cout << s2.substr(0, n-1)
             << s1.substr(l - i, l)
             << endl;
    }
    cout << s1.substr(0, n - 1)
         << s1.substr(l - n, n)
         << endl;
    for (i = 1; i < n; ++i) {
        cout << s2.substr(0, i)
             << s1.substr(0, n - i)
             << endl;
    }
}