原文:

给定一个字符串,在矩阵中打印出来,这样就形成了一个加号。

示例:

input: top
output:  
x    t    x
t    o    p
x    p    x
input: fever
output:
x    x    f    x    x
x    x    e    x    x
f    e    v    e    r
x    x    e    x    x
x    x    r    x    x

进场: 思路很简单。首先,我们可以访问矩阵的每个元素,并使其为“x”。然后我们将在矩阵的中间行和中间列插入字符串的字符。例如,我们有一个长度为 5 的字符串。所以我们需要一个(5x5)矩阵。

为了访问矩阵的中间列,列索引被设为常数,并等于(n/2),其中 n 是字符串的长度。它的行索引将从 0 变为(n-1)。 要访问中间行,行索引将被设为常量并等于(n/2),列索引将从 0 变为(n-1)。

下面是上述方法的实现:

c

// cpp program to print the 
// string in 'plus' pattern
#include 
#define max 100
using namespace std;
// function to make a cross in the matrix
void carvecross(string str)
{ 
    int n = str.length();
    if (n % 2 == 0) 
    {  
        /* as, it is not possible to make 
        the cross exactly in the middle of 
        the matrix with an even length string.*/
        cout << "not possible. please enter "
             << "odd length string.\n";
    }
    else {
        // declaring a 2d array i.e a matrix
        char arr[max][max]; 
        int m = n / 2;
        /* now, we will fill all the 
        elements of the array with 'x'*/
        for (int i = 0; i < n; i  ) {
            for (int j = 0; j < n; j  ) {
                arr[i][j] = 'x';
            }
        }
        /* now, we will place the characters 
        of the string in the matrix, such 
        that a cross is formed in it.*/
        for (int i = 0; i < n; i  ) 
        { 
            /* here the characters of the 
            string will be added in the 
            middle column of our array.*/
            arr[i][m] = str[i];
        }
        for (int i = 0; i < n; i  ) 
        { 
            // here the characters of the 
            // string will be added in the 
            // middle row of our array.
            arr[m][i] = str[i];
        }
        /* now finally, we will print 
        the array*/
        for (int i = 0; i < n; i  ) {
            for (int j = 0; j < n; j  ) {
                cout << arr[i][j] << " ";
            }
            cout << "\n";
        }
    }
}
// driver code
int main()
{
    string str = "picture";
    carvecross(str);
    return 0;
}

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

// java program to print the
// string in 'plus' pattern
class gfg {
static final int max = 100;
// function to make a cross in the matrix
static void carvecross(string str) {
    int n = str.length();
    if (n % 2 == 0) {
        // as, it is not possible to make
        // the cross exactly in the middle of
        // the matrix with an even length string.
        system.out.print("not possible. please enter "
                               "odd length string.\n");
    } 
    else {
        // declaring a 2d array i.e a matrix
        char arr[][] = new char[max][max];
        int m = n / 2;
        // now, we will fill all the
        // elements of the array with 'x'
        for (int i = 0; i < n; i  ) 
        {
            for (int j = 0; j < n; j  ) 
            {
                arr[i][j] = 'x';
        }
    }
    // now, we will place the characters
    // of the string in the matrix, such
    // that a cross is formed in it.
    for (int i = 0; i < n; i  ) {
        // here the characters of the
        // string will be added in the
        // middle column of our array.
        arr[i][m] = str.charat(i);
    }
    for (int i = 0; i < n; i  ) {
        // here the characters of the
        // string will be added in the
        // middle row of our array.
        arr[m][i] = str.charat(i);
    }
    // now finally, we will print
    // the array
    for (int i = 0; i < n; i  )
    {
        for (int j = 0; j < n; j  )
        {
            system.out.print(arr[i][j]   " ");
        }
        system.out.print("\n");
    }
    }
}
// driver code
public static void main(string[] args) {
    string str = "picture";
    carvecross(str);
}
}
// this code is contributed by anant agarwal.

python 3

# python 3 program to print the 
# string in 'plus' pattern
max = 100
# function to make a cross 
# in the matrix
def carvecross(str):
    n = len(str)
    if (n % 2 == 0) :
        ''' as, it is not possible to make 
        the cross exactly in the middle of 
        the matrix with an even length string.'''
        print("not possible. please enter "
            , "odd length string.\n")
    else :
        # declaring a 2d array i.e a matrix
        arr = [[ false for x in range(max)] 
                       for y in range(max)] 
        m = n // 2
        ''' now, we will fill all the 
        elements of the array with 'x'''
        for i in range( n) :
            for j in range(n) :
                arr[i][j] = 'x'
        '''now, we will place the characters 
        of the string in the matrix, such 
        that a cross is formed in it.'''
        for i in range(n):
            ''' here the characters of the 
            string will be added in the 
            middle column of our array.'''
            arr[i][m] = str[i]
        for i in range(n):
            # here the characters of the 
            # string will be added in the 
            # middle row of our array.
            arr[m][i] = str[i]
        # now finally, we will print 
        # the array
        for i in range(n):
            for j in range(n):
                print( arr[i][j] , end=" ")
            print()
# driver code
if __name__ == "__main__":
    str = "picture"
    carvecross(str)
# this code is contributed 
# by chitranayal

c

// c# program to print the
// string in 'plus' pattern
using system;
class gfg {
static  int max = 100;
// function to make a cross in the matrix
static void carvecross(string str) {
    int n = str.length;
    if (n % 2 == 0) {
        // as, it is not possible to make
        // the cross exactly in the middle of
        // the matrix with an even length string.
        console.write("not possible. please enter "
                              "odd length string.");
    } 
    else {
        // declaring a 2d array i.e a matrix
        char [,]arr = new char[max,max];
        int m = n / 2;
        // now, we will fill all the
        // elements of the array with 'x'
        for (int i = 0; i < n; i  ) 
        {
            for (int j = 0; j < n; j  ) 
            {
                arr[i,j] = 'x';
        }
    }
    // now, we will place the characters
    // of the string in the matrix, such
    // that a cross is formed in it.
    for (int i = 0; i < n; i  ) {
        // here the characters of the
        // string will be added in the
        // middle column of our array.
        arr[i,m] = str[i];
    }
    for (int i = 0; i < n; i  ) {
        // here the characters of the
        // string will be added in the
        // middle row of our array.
        arr[m,i] = str[i];
    }
    // now finally, we will print
    // the array
    for (int i = 0; i < n; i  )
    {
        for (int j = 0; j < n; j  )
        {
            console.write(arr[i,j]   " ");
        }
           console.writeline();
    }
    }
}
// driver code
public static void main() {
    string str = "picture";
    carvecross(str);
}
}
// this code is contributed by vt_m.

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


output:

x x x p x x x 
x x x i x x x 
x x x c x x x 
p i c t u r e 
x x x u x x x 
x x x r x x x 
x x x e x x x