📌  相关文章
📜  在不使用任何循环的情况下打印图案

📅  最后修改于: 2021-04-24 18:23:06             🧑  作者: Mango

给定数字n,请遵循模式进行打印,而不使用任何循环。

例子 :

Input: n = 16
Output: 16, 11, 6, 1, -4, 1, 6, 11, 16

Input: n = 10
Output: 10, 5, 0, 5, 10

我们基本上首先将1减5,直到达到负数或0。达到0或负数后,我们加5直到达到n。
资料来源:Microsoft面试问题。

我们强烈建议您单击此处并进行实践,然后再继续解决方案。

这个想法是使用递归。自己尝试是一个有趣的问题。
以下是代码。该代码使用标志变量来指示我们是向0移动还是向n反向移动。

C++
// C++ program to print pattern that first reduces 5 one
// by one, then adds 5. Without any loop
#include 
using namespace std;
 
// Recursive function to print the pattern.
// n indicates input value
// m indicates current value to be printed
// flag indicates whether we need to add 5 or
// subtract 5. Initially flag is true.
void printPattern(int n, int m, bool flag)
{
    // Print m.
    cout << m << " ";
     
    // If we are moving back toward the n and
    // we have reached there, then we are done
    if (flag == false && n ==m)
        return;
     
    // If we are moving toward 0 or negative.
    if (flag)
    {
    // If m is greater, then 5, recur with true flag
    if (m-5 > 0)
        printPattern(n, m-5, true);
    else // recur with false flag
        printPattern(n, m-5, false);
    }
    else // If flag is false.
        printPattern(n, m+5, false);
}
 
// Recursive function to print the pattern
// variance where m is the input int32 value
void PrintPattern(int m)
{
    if (m > 0)
    {
        cout << m << '\n';
        PrintPattern(m - 5);
    }
 
    cout << m << '\n';
}
 
// Driver Program
int main()
{
    int n = 16;
    //printPattern(n, n, true);
    PrintPattern(n);
    return 0;
}


Java
// Java program to print pattern that first reduces 5 one
// by one, then adds 5. Without any loop
import java.io.*;
 
class GFG {
     
    // Recursive function to print the pattern.
    // n indicates input value
    // m indicates current value to be printed
    // flag indicates whether we need to add 5 or
    // subtract 5. Initially flag is true.
    static void printPattern(int n, int m, boolean flag)
    {
         
        // Print m.
        System.out.print(m + " ");
 
        // If we are moving back toward the n and
        // we have reached there, then we are done
        if (flag == false && n == m)
            return;
 
        // If we are moving toward 0 or negative.
        if (flag) {
 
            // If m is greater, then 5, recur with
            // true flag
            if (m - 5 > 0)
                printPattern(n, m - 5, true);
 
            else // recur with false flag
                printPattern(n, m - 5, false);
        }
 
        else // If flag is false.
            printPattern(n, m + 5, false);
    }
 
    // Driver Program
    public static void main(String[] args)
    {
        int n = 16;
        printPattern(n, n, true);
    }
}
// This code is contributed by vt_m


Python3
# Python program to print pattern
# that first reduces 5 one by one,
# then adds 5. Without any loop.
 
# Recursive function to print
# the pattern.n indicates
# input value m indicates
# current value to be printed
# flag indicates whether we
# need to add 5 or subtract 5.
# Initially flag is True.
def printPattern(n, m, flag):
     
    # Print m.
    print(m)
     
    # If we are moving back
    # toward the n and we
    # have reached there,
    # then we are done
    if flag == False and n == m:
        return
    # If we are moving
    # toward 0 or negative.
    if flag:
    # If m is greater, then 5,
    # recur with true flag
        if m - 5 > 0:
            printPattern(n, m - 5, True)
        else: # recur with false flag
            printPattern(n, m - 5, False)
    else: # If flag is false.
        printPattern(n, m + 5, False)
 
# Driver Code
n = 16
printPattern(n, n, True)
 
# This code is contributed
# by HrushikeshChoudhary


C#
// C# program to print pattern that first reduces 5 one
// by one, then adds 5. Without any loop
using System;
 
class GFG {
     
    // Recursive function to print the pattern.
    // n indicates input value
    // m indicates current value to be printed
    // flag indicates whether we need to add 5 or
    // subtract 5. Initially flag is true.
    static void printPattern(int n, int m, bool flag)
    {
         
        // Print m.
        Console.Write(m + " ");
 
        // If we are moving back toward the n and
        // we have reached there, then we are done
        if (flag == false && n == m)
            return;
 
        // If we are moving toward 0 or negative.
        if (flag) {
 
            // If m is greater, then 5, recur with
            // true flag
            if (m - 5 > 0)
                printPattern(n, m - 5, true);
 
            else // recur with false flag
                printPattern(n, m - 5, false);
        }
 
        else // If flag is false.
            printPattern(n, m + 5, false);
    }
 
    // Driver Program
    public static void Main()
    {
        int n = 16;
        printPattern(n, n, true);
    }
}
// This code is contributed by vt_m


PHP
 0)
        printPattern($n, $m - 5, true);
     
    // recur with false flag
    else
        printPattern($n, $m - 5, false);
    }
     
    // If flag is false.
    else
        printPattern($n, $m + 5, false);
}
 
// Driver Code
$n = 16;
printPattern($n, $n, true);
 
// This code is conntributed by m_kit
?>


Javascript


C++
// C++ program to print pattern that first reduces 5 one
// by one, then adds 5. Without any loop an extra variable.
#include 
using namespace std;
 
// Recursive function to print the pattern without any extra
// variable
void printPattern(int n)
{
    // Base case (When n becomes 0 or negative)
    if (n ==0 || n<0)
    {
        cout << n << " ";
        return;
    }
     
    // First print decreasing order
    cout << n << " ";
    printPattern(n-5);
 
    // Then print increasing order
    cout << n << " ";
}
 
// Driver Program
int main()
{
    int n = 16;
    printPattern(n);
    return 0;
}


Java
// Java program to print pattern that first
// reduces 5 one by one, then adds 5.
// Without any loop an extra variable.
 
import java.io.*;
 
class GFG {
     
    // Recursive function to print the
    // pattern without any extra variable
    static void printPattern(int n)
    {
         
        // Base case (When n becomes 0 or
        // negative)
        if (n == 0 || n < 0) {
             
            System.out.print(n + " ");
             
            return;
        }
 
        // First print decreasing order
        System.out.print(n + " ");
         
        printPattern(n - 5);
 
        // Then print increasing order
        System.out.print(n + " ");
    }
 
    // Driver Program
    public static void main(String[] args)
    {
         
        int n = 16;
         
        printPattern(n);
    }
}
 
// This code is contributed by vt_m


Python3
# Python3 program to print pattern that
# first reduces 5 one by one, then adds 5.
# Without any loop an extra variable.
 
# Recursive function to print the pattern
# without any extra variable
def printPattern(n):
 
    # Base case (When n becomes 0 or negative)
    if (n == 0 or n < 0):
        print(n, end = ", ")
        return
     
    # First print decreasing order
    print(n, end = ", ")
    printPattern(n - 5)
 
    # Then print increasing order
    print(n, end = ", ")
 
# Driver Code
n = 16
printPattern(n)
 
# This code is contributed by
# Mohit kumar 29


C#
// C# program to print pattern that first
// reduces 5 one by one, then adds 5.
// Without any loop an extra variable.
 
using System;
 
class GFG {
     
    // Recursive function to print the
    // pattern without any extra variable
    static void printPattern(int n)
    {
         
        // Base case (When n becomes 0 or
        // negative)
        if (n == 0 || n < 0) {
             
            Console.Write(n + " ");
             
            return;
        }
 
        // First print decreasing order
        Console.Write(n + " ");
         
        printPattern(n - 5);
 
        // Then print increasing order
        Console.Write(n + " ");
    }
 
    // Driver Program
    public static void Main()
    {
         
        int n = 16;
         
        printPattern(n);
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


输出 :

16, 11, 6, 1, -4, 1, 6, 11, 16

如何在没有任何额外变量和循环的情况下在图案上方打印?
上面的程序可以正常工作并打印出所需的输出,但要使用额外的变量。我们可以使用两个打印语句。递归调用之前的第一个,它打印所有递减的序列。递归调用打印递增序列后的第二个。

以下是该想法的实现。

C++

// C++ program to print pattern that first reduces 5 one
// by one, then adds 5. Without any loop an extra variable.
#include 
using namespace std;
 
// Recursive function to print the pattern without any extra
// variable
void printPattern(int n)
{
    // Base case (When n becomes 0 or negative)
    if (n ==0 || n<0)
    {
        cout << n << " ";
        return;
    }
     
    // First print decreasing order
    cout << n << " ";
    printPattern(n-5);
 
    // Then print increasing order
    cout << n << " ";
}
 
// Driver Program
int main()
{
    int n = 16;
    printPattern(n);
    return 0;
}

Java

// Java program to print pattern that first
// reduces 5 one by one, then adds 5.
// Without any loop an extra variable.
 
import java.io.*;
 
class GFG {
     
    // Recursive function to print the
    // pattern without any extra variable
    static void printPattern(int n)
    {
         
        // Base case (When n becomes 0 or
        // negative)
        if (n == 0 || n < 0) {
             
            System.out.print(n + " ");
             
            return;
        }
 
        // First print decreasing order
        System.out.print(n + " ");
         
        printPattern(n - 5);
 
        // Then print increasing order
        System.out.print(n + " ");
    }
 
    // Driver Program
    public static void main(String[] args)
    {
         
        int n = 16;
         
        printPattern(n);
    }
}
 
// This code is contributed by vt_m

Python3

# Python3 program to print pattern that
# first reduces 5 one by one, then adds 5.
# Without any loop an extra variable.
 
# Recursive function to print the pattern
# without any extra variable
def printPattern(n):
 
    # Base case (When n becomes 0 or negative)
    if (n == 0 or n < 0):
        print(n, end = ", ")
        return
     
    # First print decreasing order
    print(n, end = ", ")
    printPattern(n - 5)
 
    # Then print increasing order
    print(n, end = ", ")
 
# Driver Code
n = 16
printPattern(n)
 
# This code is contributed by
# Mohit kumar 29

C#

// C# program to print pattern that first
// reduces 5 one by one, then adds 5.
// Without any loop an extra variable.
 
using System;
 
class GFG {
     
    // Recursive function to print the
    // pattern without any extra variable
    static void printPattern(int n)
    {
         
        // Base case (When n becomes 0 or
        // negative)
        if (n == 0 || n < 0) {
             
            Console.Write(n + " ");
             
            return;
        }
 
        // First print decreasing order
        Console.Write(n + " ");
         
        printPattern(n - 5);
 
        // Then print increasing order
        Console.Write(n + " ");
    }
 
    // Driver Program
    public static void Main()
    {
         
        int n = 16;
         
        printPattern(n);
    }
}
 
// This code is contributed by vt_m

的PHP


Java脚本


输出:

16, 11, 6, 1, -4, 1, 6, 11, 16