用*打印X

时间:2015-12-08 18:15:52

标签: c++ c++11 visual-c++ clion visual-c++-2012

我想用*打印X,我已经完成了X的左侧但我不知道如何打印另一面(翻转/镜像)。 如果你运行这个代码,它将只打印(X)的左侧,现在我想打印(X)的右侧?那么我该怎么做才能用星星(*)完成(X)?感谢你们。 我想知道是否可以这样做?(我是编程的新手)

#include <iostream>

// Expected output pattern:
//
//    *     *
//     *   *
//      * *
//       *
//      * *
//     *   *
//    *     *

using namespace std;

int main() {
   cout << "Printing X with star(*)" << endl;
   cout << endl;
   int i;
   int p;
   for (i = 1; i <= 10; i++) {

      for (int j = 1; j <= 10; j++) {
         if (j > i) break;
         cout << " ";
         cout << "\t";
      }
      cout << "\t\t\t\t";
      for (p = 1; p <= 10; p++) {

         cout << "*";
      }
      cout << endl;

   }
   for (i = 10; i >= 1; i--) {
      for (int j = 1; j <= 10; j++) {
         if (j > i) break;
         cout << " ";
         cout << "\t";
      }
      cout << "\t\t\t\t";
      for (p = 1; p <= 10; p++) {
         cout << "*";

      }
      cout << endl;
   }
   return 0;
}

2 个答案:

答案 0 :(得分:0)

你走在正确的轨道上,做右手边你必须在每条线上打印更多****以及你已经做过的事情。考虑打印X的每一行可能有助于打印一些****然后一些空格然后更多****并且每次接近交叉点时减少空格的数量。那有意义吗?这可能有助于你进一步发展(。=空间):

*......*
.*....*
..*..*
...**

等等

这是您可以通过的众多方式之一:

int main()
{
    int size = 8;
    int spacesBefore;
    int spacesBetween = size;
    int numStars = 1;
    // Top half:
    int i, j;
    for ( i = 0; i < size/2; i++ ) {
        spacesBetween = size - ( 2 * ( i + 1 ) );
        spacesBefore = i;
        for ( j = 0; j < spacesBefore; j++ )    // before
            cout << " ";
        for ( j = 0; j < numStars; j++ )        // * left
            cout << "*";
        for ( j  = 0; j < spacesBetween; j++ )  // between
            cout << " ";
        for ( j = 0; j < numStars; j++ )        // * right
            cout << "*";
        cout << endl;
    }
    // bottom half, do the same kind of thing but changing the spacings
    // ...
}

答案 1 :(得分:0)

好的,谢谢你们每一个帮助我的人,我在6个小时后找到了我想要的答案,这就是答案:

#include <iostream>

using namespace std;

int main() {
cout << "Printing X with stars" << endl;
cout << endl;
int i;
int p;
int k;
int s;
int count = 72;
for (i = 1; i <= 10; i++) {

    for (int j = 1; j <= 10; j++) {
        if (j > i) break;
        cout << " ";
        cout << "\t";
    }
    cout << "\t\t\t\t";
    for (p = 1; p <= 10; p++) {

        cout << "* ";
    }
    for (k=1; k<=count; k++){
        cout << " ";

    }

    count-=8;
    for (s=1; s<=10; s++){
        cout << "* ";
    }
    cout << endl;
}
count = 0;
    for (i = 10; i >= 1; i--) {
        for (int j = 1; j <= 10; j++) {
            if (j > i) break;
            cout << " ";
            cout << "\t";
        }
        cout << "\t\t\t\t";
        for (p = 1; p <= 10; p++) {
            cout << "* ";
        }
        for (k=1; k<=count; k++) {
            cout << " ";
        }
        count +=8;

        for (s=1; s<=10; s++){
            cout << "* ";
        }
        cout << endl;
        if (count == 80) break;
}
return 0;
}