📜  程序打印步骤图案

📅  最后修改于: 2021-05-31 19:42:25             🧑  作者: Mango

程序必须接受字符串S和整数N作为输入。该程序必须打印所需的图案,如下所示:
例子:

方法

  1. 设置一个标志来表示是递增还是递减
  2. 设置变量x表示要打印的* s数量,并将其初始化为0
  3. 遍历字符串的所有字符
  4. 每个字符打印x * s
  5. 如果设置了标志,则增加
  6. 否则递减
  7. 如果x值等于n-1,则将标志设置为false
  8. 如果x值等于0,则将标志设置为true

执行:

C++
// C++ program to print Step Pattern
#include 
 
using namespace std;
 
// function to print the steps
void steps(string str, int n)
{
    // declare a flag
    bool flag;
    int x = 0;
 
    // traverse through all the characters in the string
    for (int i = 0; i < str.length(); i++) {
 
        // if the x value is 0.. then
        // we must increment till n ...
        // set flag to true
        if (x == 0)
            flag = true;
 
        // if the x value is n-1 then
        // we must decrement till 0 ...
        // set flag as false
        if (x == n - 1)
            flag = false;
 
        // print x *s
        for (int j = 0; j < x; j++)
            cout << "*";
 
        cout << str[i] << "\n";
 
        // checking whether to
        // increment or decrement x
        if (flag == true)
            x++;
        else
            x--;
    }
}
 
int main()
{
 
    // Get the String and the number n
    int n = 4;
    string str = "GeeksForGeeks";
 
    cout << "String: " << str << endl;
    cout << "Max Length of Steps: "
         << n << endl;
 
    // calling the function
    steps(str, n);
 
    return 0;
}


Java
// Java Program to print Step Pattern
import java.util.*;
 
class solution
{
 
// function to print the steps
static void steps(String str, int n)
{
    // declare a flag
    boolean flag = false;
    int x = 0;
 
    // traverse through all the characters in the string
    for (int i = 0; i < str.length(); i++) {
 
        // if the x value is 0.. then
        // we must increment till n ...
        // set flag to true
        if (x == 0)
            flag = true;
 
        // if the x value is n-1 then
        // we must decrement till 0 ...
        // set flag as false
        if (x == n - 1)
            flag = false;
 
        // print x *s
        for (int j = 0; j < x; j++)
            System.out.print("*");
 
        System.out.print(str.charAt(i)+"\n");
 
        // checking whether to
        // increment or decrement x
        if (flag == true)
            x++;
        else
            x--;
    }
}
 
public static void main(String args[])
{
 
    // Get the String and the number n
    int n = 4;
    String str = "GeeksForGeeks";
 
    System.out.println("String: "+str);
    System.out.println("Max Length of Steps: "+n);
 
    // calling the function
    steps(str, n);
 
}
}
 
// This code is contributed by
// Shashank_Sharma


Python3
# Python3 program to print Step Pattern
import math as mt
 
# function to print the steps
def steps(string, n):
     
    # declare a flag
    flag = False
    x = 0
 
    # traverse through all the characters
    # in the string
    for i in range(len(string)):
 
        # if the x value is 0.. then
        # we must increment till n ...
        # set flag to true
        if (x == 0):
            flag = True
 
        # if the x value is n-1 then
        # we must decrement till 0 ...
        # set flag as false
        if (x == n - 1):
            flag = False
 
        # print x *s
        for j in range(x):
            print("*", end = "")
 
        print(string[i])
 
        # checking whether to
        # increment or decrement x
        if (flag == True):
            x += 1
        else:
            x -= 1
 
# Driver code
 
# Get the String and the number n
n = 4
string = "GeeksForGeeks"
 
print("String: ", string)
print("Max Length of Steps: ", n)
 
# calling the function
steps(string, n)
 
# This code is contributed
# by Mohit kumar 29


C#
using System;
 
// C# Program to print Step Pattern
 
public class solution
{
 
// function to print the steps
public static void steps(string str, int n)
{
    // declare a flag
    bool flag = false;
    int x = 0;
 
    // traverse through all the characters in the string
    for (int i = 0; i < str.Length; i++)
    {
 
        // if the x value is 0.. then
        // we must increment till n ...
        // set flag to true
        if (x == 0)
        {
            flag = true;
        }
 
        // if the x value is n-1 then
        // we must decrement till 0 ...
        // set flag as false
        if (x == n - 1)
        {
            flag = false;
        }
 
        // print x *s
        for (int j = 0; j < x; j++)
        {
            Console.Write("*");
        }
 
        Console.Write(str[i] + "\n");
 
        // checking whether to
        // increment or decrement x
        if (flag == true)
        {
            x++;
        }
        else
        {
            x--;
        }
    }
}
 
// Driver code
public static void Main(string[] args)
{
 
    // Get the String and the number n
    int n = 4;
    string str = "GeeksForGeeks";
 
    Console.WriteLine("String: " + str);
    Console.WriteLine("Max Length of Steps: " + n);
 
    // calling the function
    steps(str, n);
 
}
}
 
  // This code is contributed by shrikant13


PHP


Javascript


输出:
String: GeeksForGeeks
Max Length of Steps: 4
G
*e
**e
***k
**s
*F
o
*r
**G
***e
**e
*k
s
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”