原文:

给定奇数长度的字符串,以 x 格式打印该字符串。 例:

input: 12345
output:
1       5
  2   4
    3
  2   4
1       5 
input: geeksforgeeks
output:
g                         s
  e                     k
    e                 e
      k             e
        s         g
          f      r
             o
          f     r
        s         g
      k             e
    e                 e
  e                      k
g                          s 

我们强烈建议你尽量减少浏览器,先自己试试这个。 想法是在单个循环中使用两个变量,第一个变量‘i’从左到右,第二个变量‘j’从右到左。十字(或 x)的上半部分在它们相遇之前被打印出来。中心字符在它们相遇时被打印,下分字符在它们交叉后被打印。在上部,str[i]打印在 str[j]之前,在下部,str[j]打印在 str[i]之前。 以下是以上思路的实现。

c

// c   program to print cross pattern
#include
using namespace std;
// function to print given string in cross pattern
// length of string must be odd
void printpattern(string str)
{
    int len = str.length();
    // i goes from 0 to len and j goes from len-1 to 0
    for (int i=0,j=len-1; i<=len,j>=0; i  ,j--)
    {
        // to print the upper part. this loop runs
        // till middle point of string (i and j become
        // same
        if (ij)
        {
            // print j spaces
            for (int x = j-1; x>=0; x--)
                cout << " ";
            // print j'th character
            cout << str[j];
            // print i-j-1 spaces
            for (int x=0; x

java 语言(一种计算机语言,尤用于创建网站)

// java program to
// print cross pattern
class gfg
{
// function to print given
// string in cross pattern
static void pattern(string str,
                    int len)
{
    // i and j are the indexes
    // of characters to be
    // displayed in the ith
    // iteration i = 0 initially
    // and go upto length of string
    // j = length of string initially
    // in each iteration of i,
    // we increment i and decrement j,
    // we print character only
    // of k==i or k==j
    for (int i = 0; i < len; i  )
    {
        int j = len - 1 - i;
        for (int k = 0; k < len; k  )
        {
            if (k == i || k == j)
                system.out.print(str.charat(k));
            else
                system.out.print(" ");
        }
        system.out.println("");
    }
}
// driver code
public static void main (string[] args)
{
    string str = "geeksforgeeks";
    int len = str.length();
    pattern(str, len);
}
}
// this code is contributed
// by smitha

python 3

# python 3 program to
# print cross pattern
# function to print given
# string in cross pattern
def pattern(str, len):
    # i and j are the indexes
    # of characters to be
    # displayed in the ith
    # iteration i = 0 initially
    # and go upto length of string
    # j = length of string initially
    # in each iteration of i, we
    # increment i and decrement j,
    # we print character only of
    # k==i or k==j
    for i in range(0, len):
        j = len -1 - i
        for k in range(0, len):
            if (k == i or k == j):
                print(str[k],
                      end = "")
            else:
                print(end = " ")
        print(" ")
# driver code
str = "geeksforgeeks"
len = len(str)
pattern(str, len)
# this code is contributed
# by smitha

c

// c# program to print
// cross pattern
using system;
class gfg
{
// function to print given
// string in cross pattern
static void pattern(string str,
                    int len)
{
    // i and j are the indexes
    // of characters to be
    // displayed in the ith
    // iteration i = 0 initially
    // and go upto length of string
    // j = length of string initially
    // in each iteration of i, we
    // increment i and decrement j,
    // we print character only of
    // k==i or k==j
    for (int i = 0; i < len; i  )
    {
        int j = len - 1 - i;
        for (int k = 0; k < len; k  )
        {
            if (k == i || k == j)
                console.write(str[k]);
            else
                console.write(" ");
        }
        console.write("\n");
    }
}
// driver code
public static void main ()
{
    string str = "geeksforgeeks";
    int len = str.length;
    pattern(str, len);
}
}
// this code is contributed by smitha

服务器端编程语言(professional hypertext preprocessor 的缩写)

= 0;
         $i  , $j--)
    {
        // to print the upper part.
        // this loop runs till middle point
        // of string i and j become same
        if ($i < $j)
        {
            // print i spaces
            for ($x = 0; $x < $i; $x  )
                echo " ";
            // print i'th character
            echo $str[$i];
            // print j-i-1 spaces
            for ( $x = 0; $x < $j - $i - 1;
                                      $x  )
                echo " ";
            // print j'th character
            echo $str[$j]."\n";
        }
        // to print center point
        if ($i == $j)
        {
            // print i spaces
            for ($x = 0; $x < $i; $x  )
                echo " ";
            // print middle character
            echo $str[$i]."\n";
        }
        // to print lower part
        else if ($i > $j)
        {
            // print j spaces
            for ($x = $j - 1; $x >= 0;
                                 $x--)
                echo " ";
            // print j'th character
            echo $str[$j];
            // print i-j-1 spaces
            for ( $x = 0; $x < $i - $j - 1;
                                      $x  )
                echo " ";
            // print i'h character
            echo $str[$i]."\n";
        }
    }
}
// driver code
printpattern("geeksforgeeks");
// this code is contributed by mits
?>

java 描述语言


输出:

g           s
 e         k
  e       e
   k     e
    s   g
     f r
      o
     f r
    s   g
   k     e
  e       e
 e         k
g           s

备选方案:

c

// cpp program to print cross pattern
#include
using namespace std;
// function to print given string in
// cross pattern
void pattern(string str, int len){
    // i and j are the indexes of characters
    // to be displayed in the ith iteration
    // i = 0 initially and go upto length of
    // string
    // j = length of string initially
    // in each iteration of i, we increment
    // i and decrement j, we print character
    // only of k==i or k==j
    for (int i = 0; i < len; i  )
    {
        int j = len -1 - i;
        for (int k = 0; k < len; k  )
        {
            if (k == i || k == j)
                cout << str[k];
            else
                cout << " ";
        }
        cout << endl;      
    }
}
// driver code
int main ()
{
    string str = "geeksforgeeks";
    int len = str.size();
    pattern(str, len);
    return 0;
}
// this code is contributed by satinder kaur

java 语言(一种计算机语言,尤用于创建网站)

// java program to print cross pattern
class gfg
{
// function to print given 
// string in cross pattern
static void pattern(string str, int len)
{
    // i and j are the indexes of
    // characters  to be displayed
    // in the ith iteration i = 0
    // initially and go upto length
    // of string j = length of string 
    // initially in each iteration
    // of i, we increment i and decrement
    // j, we print character only
    // of k==i or k==j
    for (int i = 0; i < len; i  )
    {
        int j = len -1 - i;
        for (int k = 0; k < len; k  )
        {
            if (k == i || k == j)
                system.out.print(str.charat(k));
            else
                system.out.print(" ");
        }
        system.out.println("");    
    }
}
// driver code
public static void main(string[] args)
{
    string str = "geeksforgeeks";
    int len = str.length();
    pattern(str, len);
}
}
// this code is contributed by 29ajaykumar

python 3

# python 3 program to print cross pattern
# function to print given string in
# cross pattern
def pattern(st, length):
    # i and j are the indexes of characters
    # to be displayed in the ith iteration
    # i = 0 initially and go upto length of
    # string
    # j = length of string initially
    # in each iteration of i, we increment
    # i and decrement j, we print character
    # only of k==i or k==j
    for i in range(length):
        j = length -1 - i
        for k in range(length):
            if (k == i or k == j):
                print(st[k],end="")
            else:
                print(" ",end="")
        print()
# driver code
if __name__ == "__main__":
    st = "geeksforgeeks"
    length = len(st)
    pattern(st, length)

c

// c# program to print cross pattern
using system;
class gfg
{
// function to print given
// string in cross pattern
static void pattern(string str, int len)
{
    // i and j are the indexes of
    // characters to be displayed
    // in the ith iteration i = 0
    // initially and go upto length
    // of string j = length of string
    // initially in each iteration
    // of i, we increment i and decrement
    // j, we print character only
    // of k==i or k==j
    for (int i = 0; i < len; i  )
    {
        int j = len -1 - i;
        for (int k = 0; k < len; k  )
        {
            if (k == i || k == j)
                console.write(str[k]);
            else
                console.write(" ");
        }
        console.writeline("");    
    }
}
// driver code
public static void main(string[] args)
{
    string str = "geeksforgeeks";
    int len = str.length;
    pattern(str, len);
}
}
// this code is contributed by rajput-ji

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


输出:

g           s
 e         k
  e       e
   k     e
    s   g
     f r
      o
     f r
    s   g
   k     e
  e       e
 e         k
g           s

pg电子试玩链接的解决方案 3 :这个问题也可以通过观察字符是沿着左右对角线打印的,只要我们把图案封装在一个矩阵内就可以解决。现在,如果字符串的长度是 len ,那么该模式可以包含在有序的方形矩阵 len 中。

  • 左对角线上的元素可以通过条件 ( i==j ) 来访问,其中 i 和 j 分别是行号和列号。
  • 右对角线上的元素可以通过条件 (i j == len-1) 访问。

因此,运行顺序为 len 的嵌套循环,用各自的字符填充满足上述两个条件的位置,并用空格填充其余位置。 以下是上述办法的实施情况:

卡片打印处理机(card print processor 的缩写)

// c   program to print the given pattern
#include
using namespace std;
// function to print the given
// string in respective pattern
void printpattern(string str, int len)
{  
    for(int i = 0; i < len; i  )
    {
        for(int j = 0; j < len; j  )
        {  
            // print characters at corresponding
            // places satisfying the two conditions
            if((i == j) || (i   j == len-1))
                cout<

java 语言(一种计算机语言,尤用于创建网站)

// java program to print the given pattern
import java.io.*;
class gfg
{
  // function to print the given
  // string in respective pattern
  static void printpattern(string str, int len)
  {
    for(int i = 0; i < len; i  )
    {
      for(int j = 0; j < len; j  )
      {  
        // print characters at corresponding
        // places satisfying the two conditions
        if((i == j) || (i   j == len - 1))
          system.out.print(str.charat(j));
        // print blank space at rest of places
        else
          system.out.print(" ");
      }
      system.out.println();
    }
  }
  // driver code
  public static void main (string[] args)
  {   
    string str = "geeksforgeeks";
    int len = str.length();
    printpattern(str, len);
  }
}
// this code is contributed by rag2127.

python 3

# python3 program to print the given pattern
# function to print the given
# string in respective pattern
def printpattern (str, len) :
    for i in range(len) :
        for j in range(len) :
            # print characters at corresponding
            # places satisfying the two conditions
            if ((i == j) or (i   j == len - 1)) :
                print(str[j], end = "")
            # print blank space at rest of places
            else :
                print(" ", end = "")
        print()
str = "geeksforgeeks"
len = len(str)
printpattern(str, len)
# this code is contributed by divyeshrabadiya07.

c

// c# program to print the given pattern
using system;
public class gfg
{
  // function to print the given
  // string in respective pattern
  static void printpattern(string str, int len)
  {
    for(int i = 0; i < len; i  )
    {
      for(int j = 0; j < len; j  )
      {  
        // print characters at corresponding
        // places satisfying the two conditions
        if((i == j) || (i   j == len - 1))
          console.write(str[j]);
        // print blank space at rest of places
        else
          console.write(" ");
      }
      console.writeline();
    }
  }
  // driver code  
  static public void main ()
  {
    string str = "geeksforgeeks";
    int len = str.length;
    printpattern(str, len);
  }
}
// this code is contributed by avanitrachhadiya2155

java 描述语言


输出:

g           s
 e         k
  e       e
   k     e
    s   g
     f r
      o
     f r
    s   g
   k     e
  e       e
 e         k
g           s

https://youtu . be/7 gyjmrezos

本文由 供稿。如果你发现任何不正确的地方,请写评论,或者你想分享更多关于上面讨论的话题的信息