以垂直之字形方式打印字符串的程序
给定一个大小为N的字符串S , 和行数R ,任务是按照示例中所示的给定行数以垂直之字形方式打印给定字符串。
例子:
Input: S = “123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”, R = 9
Output:
Input: S = “AttentionReaders!Don’tStopLearning!HappyLearning!”, R = 12
Output:
方法:为了逐行打印字符,想法是找到主要列之间的间隔和中间列的步长值,以打印空格,直到到达字符串的最后一个字符。请按照以下步骤解决此问题:
- 将变量间隔初始化为2*R-2以存储主要列之间的间隙。
- 使用变量i在[0, R-1]范围内迭代
- 将变量step初始化为interval-2*i以存储每行的 step 值。
- 使用变量j在[i, N-1]范围内迭代,在每次迭代中按间隔递增j ,
- 打印字符, S[j] 。
- 如果step的值在[1, interval-1]范围内且step+j
,则打印(interval-Ri)个空格,然后打印s[j+step]最后打印(i-1)空格。 - 否则打印(间隔-R)空格数。
- 在外循环的每次迭代后打印换行符。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print any string
// in zigzag fashion
void zigzag(string s, int rows)
{
// Store the gap between the major columns
int interval = 2 * rows - 2;
// Traverse through rows
for (int i = 0; i < rows; i++) {
// Store the step value for each row
int step = interval - 2 * i;
// Iterate in the range [1, N-1]
for (int j = i; j < s.length(); j = j + interval) {
// Print the character
cout << s[j];
if (step > 0 && step < interval
&& step + j < s.length()) {
// Print the spaces before character
// s[j+step]
for (int k = 0; k < (interval - rows - i);
k++)
cout << " ";
// Print the character
cout << s[j + step];
// Print the spaces after character
// after s[j+step]
for (int k = 0; k < i - 1; k++)
cout << " ";
}
else {
// Print the spaces for first and last rows
for (int k = 0; k < (interval - rows); k++)
cout << " ";
}
}
cout << endl;
}
}
// Driver Code
int main()
{
// Given Input
string s = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh"
"ijklmnopqrstuvwxyz";
int rows = 9;
// Function Call
zigzag(s, rows);
}
Java
// Java program for the above approach
public class GFG{
// Function to print any string
// in zigzag fashion
static void zigzag(String s, int rows)
{
// Store the gap between the major columns
int interval = 2 * rows - 2;
// Traverse through rows
for(int i = 0; i < rows; i++)
{
// Store the step value for each row
int step = interval - 2 * i;
// Iterate in the range [1, N-1]
for(int j = i; j < s.length(); j = j + interval)
{
// Print the character
System.out.print(s.charAt(j));
if (step > 0 && step < interval &&
step + j < s.length())
{
// Print the spaces before character
// s[j+step]
for(int k = 0; k < (interval - rows - i); k++)
System.out.print(" ");
// Print the character
System.out.print(s.charAt(j + step));
// Print the spaces after character
// after s[j+step]
for(int k = 0; k < i - 1; k++)
System.out.print(" ");
}
else
{
// Print the spaces for first and last rows
for(int k = 0; k < (interval - rows); k++)
System.out.print(" ");
}
}
System.out.println();
}
}
// Driver Code
public static void main(String args[])
{
// Given Input
String s = "123456789ABCDEFGHIJKLM" +
"NOPQRSTUVWXYZabcdefghi" +
"jklmnopqrstuvwxyz";
int rows = 9;
// Function Call
zigzag(s, rows);
}
}
// This code is contributed by SoumikMondal
Python3
# Python3 program for the above approach
# Function to print any string
# in zigzag fashion
def zigzag(s, rows):
# Store the gap between the major columns
interval = 2 * rows - 2
# Traverse through rows
for i in range(rows):
# Store the step value for each row
step = interval - 2 * i
# Iterate in the range [1, N-1]
for j in range(i, len(s), interval):
# Print the character
print(s[j], end = "")
if (step > 0 and step < interval and
step + j < len(s)):
# Print the spaces before character
# s[j+step]
for k in range((interval - rows - i)):
print(end = " ")
# Print the character
print(s[j + step], end = "")
# Print the spaces after character
# after s[j+step]
for k in range(i - 1):
print(end = " ")
else:
# Print the spaces for first and
# last rows
for k in range(interval - rows):
print(end = " ")
print()
# Driver Code
if __name__ == '__main__':
# Given Input
s = "123456789ABCDEFGHIJKL"\
"MNOPQRSTUVWXYZabcdefghi"\
"jklmnopqrstuvwxyz"
rows = 9
# Function Call
zigzag(s, rows)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to print any string
// in zigzag fashion
static void zigzag(string s, int rows)
{
// Store the gap between the major columns
int interval = 2 * rows - 2;
// Traverse through rows
for(int i = 0; i < rows; i++)
{
// Store the step value for each row
int step = interval - 2 * i;
// Iterate in the range [1, N-1]
for(int j = i; j < s.Length; j = j + interval)
{
// Print the character
Console.Write(s[j]);
if (step > 0 && step < interval &&
step + j < s.Length)
{
// Print the spaces before character
// s[j+step]
for(int k = 0; k < (interval - rows - i); k++)
Console.Write(" ");
// Print the character
Console.Write(s[j + step]);
// Print the spaces after character
// after s[j+step]
for(int k = 0; k < i - 1; k++)
Console.Write(" ");
}
else
{
// Print the spaces for first and last rows
for(int k = 0; k < (interval - rows); k++)
Console.Write(" ");
}
}
Console.WriteLine();
}
}
// Driver Code
public static void Main()
{
// Given Input
string s = "123456789ABCDEFGHIJKLM" +
"NOPQRSTUVWXYZabcdefghi" +
"jklmnopqrstuvwxyz";
int rows = 9;
// Function Call
zigzag(s, rows);
}
}
// This code is contributed by SURENDRA_GANGWAR
Javascript
输出
1 H X n
2 GI WY mo
3 F J V Z l p
4 E K U a k q
5 D L T b j r z
6 C M S c i s y
7 B N R d h t x
8A OQ eg uw
9 P f v
时间复杂度: O(R 2 *N)
辅助空间: O(1)