📜  祝你的情人有一个节目!

📅  最后修改于: 2021-05-19 19:54:56             🧑  作者: Mango

情人节,在您的亲人的心脏上写下一条讯息,表达您对他们的感受。不要错过让他们感到惊讶的时刻。
本文演示了使他们惊讶的代码。所有<3的程序员的挚爱致以最良好的祝愿

下面是代码。

C++
// C++ code to print happy valentine's
// day.
#include 
using namespace std;
 
int main() {
 
  // initializing variables and size
  double a, b, n = 10;
 
  // initializing String to print in heart
  string message = " HappY Valentines DaY <3 ";
 
  // Position from where from top
  // message box would be placed.
  int print_message = 4;
 
  // add space if message length is odd
  if (message.length() % 2 != 0)
    message += " ";
 
  // outer loop to adjust length of upper
  // part message is not handled in this part
  for (a = 0; a < n; a++) {
 
    // to print space and variable accordingly
    for (b = 0; b <= 4 * n; b++) {
 
      // computing distance to print variable
      double distance1 = sqrt(pow(a - n, 2) + pow(b - n, 2));
      double distance2 = sqrt(pow(a - n, 2) + pow(b - 3 * n, 2));
 
      if (distance1 < n + 0.5 || distance2 < n + 0.5)
        cout << "S";
 
      else
        cout << " ";
    }
 
    // ending line after each iteration
    cout << "\n";
  }
 
  // printing the message part
  // and lower part of heart.
  // outer loop handles
  // depth of the heart.
  for (a = 1; a < 2 * n; a++) {
 
    // for getting the lower curve of heart
    for (b = 0; b < a; b++)
      cout << " ";
 
    // inner loop
    // handles the message and spaces accordingly
    for (b = 0; b < 4 * n + 1 - 2 * a; b++) {
 
      // checks if the height is in range of message
      // space
      if (a >= print_message - 1 && a <= print_message + 1) {
        int point = b - (4 * n - 2 * a - message.size()) / 2;
 
        // prints message after leaving appropriate space
        if (point < message.size() && point >= 0) {
          if (a == print_message)
            cout << message[point];
          else
            cout << " ";
        }
 
        else
          cout << "S";
      }
 
      else
        cout << "S";
    }
 
    cout << endl;
  }
}


Java
// Java code to print happy valentine's day.
class GFG {
     
    public static void main(String[] args) {
         
         
        // initializing variables and size
        double a, b, n = 10;
     
        // initializing String to print in heart
        String message = " HappY Valentines DaY <3 ";
     
        // Position from where from top
        // message box would be placed.
        int print_message = 4;
     
        // add space if message length is odd
        if (message.length() % 2 != 0)
            message += " ";
     
        // outer loop to adjust length of upper
        // part message is not handled in this part
        for (a = 0; a < n; a++) {
     
            // to print space and variable accordingly
            for (b = 0; b <= 4 * n; b++) {
         
                // computing distance to print variable
                double distance1 = Math.sqrt(Math.pow(a - n, 2)
                                         + Math.pow(b - n, 2));
                double distance2 = Math.sqrt(Math.pow(a - n, 2)
                                    + Math.pow(b - 3 * n, 2));
         
                if (distance1 < n + 0.5 || distance2 < n + 0.5)
                    System.out.print("S");
         
                else
                    System.out.print(" ");
            }
         
            // ending line after each iteration
            System.out.println();
        }
     
        // printing the message part
        // and lower part of heart.
        // outer loop handles
        // depth of the heart.
        for (a = 1; a < 2 * n; a++) {
     
            // for getting the lower curve of heart
            for (b = 0; b < a; b++)
                System.out.print(" ");
         
            // inner loop handles the message
            // and spaces accordingly
            for (b = 0; b < 4 * n + 1 - 2 * a; b++) {
         
                // checks if the height is in range
                // of message space
                if (a >= print_message - 1 &&
                             a <= print_message + 1) {
                     
                    double point = b - (4 * n - 2 * a
                                 - message.length()) / 2;
             
                    // prints message after leaving
                    // appropriate space
                    if (point < message.length() &&
                                          point >= 0) {
                        if (a == print_message)
                            System.out.print
                               (message.charAt((int)point));
                        else
                            System.out.print(" ");
                    }
             
                    else
                        System.out.print("S");
                }
         
                else
                    System.out.print("S");
            }
         
            System.out.println();
        }
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 code to print happy valentine's
# day
import math
 
n = 10
 
# Initializing String to print in heart
message = " HappY Valentines DaY <3 "
 
# Position from where from top
# message box would be placed.
print_message = 4
 
# Add space if message length is odd
if (len(message) % 2 != 0):
    message += " "
 
# Outer loop to adjust length of upper
# part message is not handled in this part
for a in range(n):
     
    # To print space and variable accordingly
    for b in range(4 * n + 1):
         
        # Computing distance to print variable
        distance1 = math.sqrt(pow(a - n, 2) +
                              pow(b - n, 2))
        distance2 = math.sqrt(pow(a - n, 2) +
                              pow(b - 3 * n, 2))
                               
        if (distance1 < n + 0.5 or
            distance2 < n + 0.5):
            print("S", end = "")
        else:
            print(" ", end = "")
 
    # Ending line after each iteration
    print()
 
# Printing the message part and lower
# part of heart. Outer loop handles
# depth of the heart.
for a in range(1, 2 * n):
 
    # For getting the lower curve of heart
    for b in range(a):
        print(" ", end = "")
         
    # Inner loop
    # handles the message and spaces accordingly
    for b in range(4 * n + 1 - 2 * a):
         
        # Checks if the height is in range of
        # message space
        if (a >= print_message - 1 and
            a <= print_message + 1):
            point = b - (4 * n - 2 *
                    a - len(message)) // 2
 
            # Prints message after leaving
            # appropriate space
            if (point < len(message) and point >= 0):
                if (a == print_message):
                    print(message[point], end = "")
                else:
                    print(" ", end = "")
            else:
                print("S", end = "")
 
        else:
            print("S", end = "")
 
    print()
 
# This code is contributed by subhammahato348


C#
// C# code to print happy valentine's day.
using System;
class GFG
{
 
    public static void Main()
    {
 
        // initializing variables and size
        double a, b, n = 10;
 
        // initializing String to print in heart
        string message = " HappY Valentines DaY <3 ";
 
        // Position from where from top
        // message box would be placed.
        int print_message = 4;
 
        // add space if message length is odd
        if (message.Length % 2 != 0)
            message += " ";
 
        // outer loop to adjust length of upper
        // part message is not handled in this part
        for (a = 0; a < n; a++) {
 
            // to print space and variable accordingly
            for (b = 0; b <= 4 * n; b++) {
 
                // computing distance to print variable
                double distance1
                    = Math.Sqrt(Math.Pow(a - n, 2)
                                + Math.Pow(b - n, 2));
                double distance2
                    = Math.Sqrt(Math.Pow(a - n, 2)
                                + Math.Pow(b - 3 * n, 2));
                if (distance1 < n + 0.5
                    || distance2 < n + 0.5)
                    Console.Write("S");
 
                else
                    Console.Write(" ");
            }
 
            // ending line after each iteration
            Console.WriteLine();
        }
 
        // printing the message part
        // and lower part of heart.
        // outer loop handles
        // depth of the heart.
        for (a = 1; a < 2 * n; a++) {
 
            // for getting the lower curve of heart
            for (b = 0; b < a; b++)
                Console.Write(" ");
 
            // inner loop handles the message
            // and spaces accordingly
            for (b = 0; b < 4 * n + 1 - 2 * a; b++)
            {
 
                // checks if the height is in range
                // of message space
                if (a >= print_message - 1
                    && a <= print_message + 1)
                {
 
                    double point = b
                          - (4 * n - 2 * a - message.Length)
                                / 2;
 
                    // prints message after leaving
                    // appropriate space
                    if (point < message.Length
                        && point >= 0) {
                        if (a == print_message)
                            Console.Write(
                                message[(int)point]);
                        else
                            Console.Write(" ");
                    }
 
                    else
                        Console.Write("S");
                }
 
                else
                    Console.Write("S");
            }
 
            Console.WriteLine();
        }
    }
}
 
// This code is contributed by subhammahato348


输出:

SSSSSSS             SSSSSSS       
     SSSSSSSSSSS         SSSSSSSSSSS     
    SSSSSSSSSSSSS       SSSSSSSSSSSSS    
   SSSSSSSSSSSSSSS     SSSSSSSSSSSSSSS   
  SSSSSSSSSSSSSSSSS   SSSSSSSSSSSSSSSSS  
 SSSSSSSSSSSSSSSSSSS SSSSSSSSSSSSSSSSSSS 
 SSSSSSSSSSSSSSSSSSS SSSSSSSSSSSSSSSSSSS 
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
 SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
  SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
   SSSS                          SSSSS
    SSS HappY Valentines DaY <3  SSSS
     SS                          SSS
      SSSSSSSSSSSSSSSSSSSSSSSSSSSSS
       SSSSSSSSSSSSSSSSSSSSSSSSSSS
        SSSSSSSSSSSSSSSSSSSSSSSSS
         SSSSSSSSSSSSSSSSSSSSSSS
          SSSSSSSSSSSSSSSSSSSSS
           SSSSSSSSSSSSSSSSSSS
            SSSSSSSSSSSSSSSSS
             SSSSSSSSSSSSSSS
              SSSSSSSSSSSSS
               SSSSSSSSSSS
                SSSSSSSSS
                 SSSSSSS
                  SSSSS
                   SSS

资料来源:

  • https://www.geeksforgeeks.org/printing-heart-pattern-c/
  • 托恩(Torne)在https://stackoverflow.com/questions/20075775/print-heart-shape-with-words-inside上的回答

注意:最好使用N至少8的值。您可以更改需要使用的字符(在上述情况下为S)。

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”