以'X'格式打印奇数长度的字符串
给定一个奇数长度的字符串,打印字符串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”从右到左。 Cross(或 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
Python3
# 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
PHP
= 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
?>
Javascript
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
Python3
# 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
PHP
Javascript
CPP
// 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.
Python3
# 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
Javascript
输出 :
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
Python3
# 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
PHP
Javascript
输出 :
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
解决方案3 :这个问题也可以通过观察字符打印在左右对角线上来解决,前提是我们将模式包含在矩阵中。现在,如果字符串的长度是len ,那么该模式可以包含在一个len阶的方阵中。
- 沿左对角线的元素可以通过条件( i==j )访问,其中 i 和 j 分别是行号和列号。
- 沿右对角线的元素可以通过条件(i+j == len-1)访问。
因此,运行一个len顺序的嵌套循环,并用相应的字符填充满足上述两个条件的位置,其余的位置用空格填充。
下面是上述方法的实现:
CPP
// 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.
Python3
# 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
Javascript
输出 :
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/7gyjMrezEos