Swift 程序打印数字沙漏图案

swiftserver side programmingprogramming更新于 2025/9/20 14:52:17

本教程将讨论如何编写 Swift 程序打印数字沙漏图案。

数字图案是一系列数字,用于创建不同的图案或形状,例如金字塔、矩形、十字形等。这些数字图案通常用于理解或练习程序流程控制,也有助于培养逻辑思维能力。

要创建数字沙漏星形图案,我们可以使用以下任何一种方法 -

  • 使用嵌套 for 循环

  • 使用 init() 函数

  • 使用 stride 函数

在这里,我们将数字沙漏图案分为两部分:上半部分和下半部分,以解决问题。

方法 1 - 使用嵌套 For 循环

我们可以使用嵌套 for 循环创建数字沙漏图案或任何其他图案。这里每个 for 循环处理不同的任务,例如保存行、列、空格等。

算法

以下是算法 -

步骤 1 - 声明变量以存储三角形图案的长度。

步骤 2 - 运行一个从 1 到 num-1 的外层 for 循环。此循环处理要打印的总行数,每行都以新行开始。

步骤 3 - 运行一个从 1 到 i 的嵌套 for 循环。此循环用于打印空格,并且在每次迭代中,空格数都会增加 1。

步骤 4 - 运行另一个从 1 到 num-i 的嵌套 for 循环。此循环用于打印上半部分的数字沙漏图案。

步骤 5 - 再次运行从 1 到 num-1 的外部 for 循环

步骤 6 - 运行从 1 到 num-m 的嵌套 for 循环。此循环用于打印空格,并且在每次迭代中空格减少 1。

步骤 7 - 运行另一个从 1 到 m 的嵌套 for 循环。此循环用于打印下半部分的数字沙漏图案。

示例

以下程序演示如何使用嵌套 for 循环打印数字沙漏图案。

import Foundation import Glibc // Length of the triangle pattern let num = 4 // Outer for loop is used to handle the total // number of rows in upper half portion for i in 1..<num{ // Nested for loop is used to print white // spaces for _ in 1...i{ print(terminator: " ") } // Nested for loop is used to print // upper half hourglass for j in 1...num-i{ print(j, terminator: " ") } // Add new line print("") } // Outer for loop is used to handle the total // number of rows in lower half portion for m in 1..<num{ // Nested for loop is used to print white // spaces for _ in 1...(num-m){ print(terminator: " ") } // Nested for loop is used to print // lower half hourglass for n in 1...m{ print(n, terminator: " ") } // Add new line print("") }

输出

 1 2 3 
  1 2 
   1 
   1 
  1 2 
 1 2 3

方法 2 - 使用 init() 函数

Swift 提供了一个名为 String.init() 的内置函数。使用此函数,我们可以创建任何模式。String.init() 函数创建一个字符串,其中给定字符重复指定次数。

语法

语法如下 -

String.init(repeating:Character, count: Int)

其中,repeating 表示此方法重复的字符,count 表示给定字符在结果字符串中重复的总次数。

示例

以下程序演示如何使用 string.init() 函数打印数字沙漏模式。

import Foundation import Glibc // Length of the triangle pattern var num = 4 var x = num // Creating upper half portion // Using String.init() function while x >= 1{ print(String.init(repeating: " ", count: num-x) + String.init(repeating: "33", count: 2*x-1)) x -= 1 } // Creating lower half portion // Using String.init() function for y in 1...num{ print(String.init(repeating: " ", count: num-y) + String.init(repeating: "44", count: 2*y-1)) }

输出

33333333333333
 3333333333
  333333
   33
   44
  444444
 4444444444
44444444444444

在上面的代码中,我们使用了一个 while 循环,条件是 x>=1。此循环使用 init() 函数打印沙漏图案的上半部分 -

while x >= 1 {
    print(String.init(repeating: " ", count: num-x) + String.init(repeating: "33", count: 2*x-1))
    x -= 1
}

这里,String.init(repeating: " ", count: num-x) 打印空格,而 String.init(repeating: "33", count: 2*x-1) 打印数字沙漏图案的上半部分。

现在运行另一个从 1 到 num 的 for 循环。此循环使用 init() 函数打印沙漏图案的下半部分 -

for y in 1...num {
    print(String.init(repeating: " ", count: num-y) + String.init(repeating: "44", count: 2*y-1))
}

此处,String.init(repeating: " ", count: num-y) 打印空格,而 String.init(repeating: "44", count: 2*y-1) 打印数字沙漏图案的下半部分。

方法 3 - 使用 stride 函数

Swift 提供了一个名为 stride() 的内置函数。stride() 函数用于以递增或递减的方式从一个值移动到另一个值。或者我们可以说 stride() 函数返回一个从起始值开始但不包含结束值的序列,并且给定序列中的每个值都按给定的量进行步进。

语法

以下是语法 −

stride(from:startValue, to: endValue, by:count)

此处,

from − 表示给定序列的起始值。

to − 表示限制给定序列的终止值。

by − 表示每次迭代的步长,此处正值表示向上迭代或增量,负值表示向下迭代或减量。

示例

以下程序演示如何使用 stride() 函数打印数字沙漏图案。

import Foundation import Glibc // 三角形形态的长度 var num = 6 // 创建上半部分 // 处理上半部分的长度 for x in 1...num-1{ // Print spaces for _ in 1...x{ print(terminator: " ") } // Print numbers from 1 to 5 for y in stride(from: x, to: num, by: 1){ print(y, terminator : " ") } // Add new lines print(" ") } // Creating lower half portion // Handle length of the lower half portion for m in stride(from:num, to: 1, by: -1){ // Print spaces for _ in 1..<m{ print(terminator : " ") } // Print numbers from 5 to 1 for n in stride(from: m-1, to: num, by:1){ print(n, terminator : " ") } // Add new line print(" ") }

输出

 1 2 3 4 5  
  2 3 4 5  
   3 4 5  
    4 5  
     5  
     5  
    4 5  
   3 4 5  
  2 3 4 5  
 1 2 3 4 5    


相关文章



关于我们 | 隐私条款 | 学习路线
返回顶部