Swift程式列印數字沙漏圖案
本教程將討論如何編寫Swift程式來列印數字沙漏圖案。
數字圖案是由數字組成的序列,用於開發不同的圖案或形狀,例如金字塔、矩形、十字架等。這些數字圖案通常用於理解或練習程式流程控制,它們也對邏輯思維很有幫助。
要建立數字沙漏星形圖案,我們可以使用以下任何一種方法:
使用巢狀for迴圈
使用init()函式
使用stride函式
我們將數字沙漏圖案分成兩部分:上半部分和下半部分來解決這個問題。
方法1 - 使用巢狀for迴圈
我們可以使用巢狀for迴圈建立數字沙漏圖案或任何其他圖案。這裡每個for迴圈處理不同的任務,例如儲存行、列、空格等。
演算法
以下是演算法:
步驟1 - 宣告變數以儲存三角形圖案的長度。
步驟2 - 從1到num-1執行外部for迴圈。此迴圈處理要列印的行總數,每行以換行符開頭。
步驟3 - 從1到i執行巢狀for迴圈。此迴圈用於列印空格,並且在每次迭代中空格增加一個。
步驟4 - 從1到num-i執行另一個巢狀for迴圈。此迴圈用於列印上半部分的數字沙漏圖案。
步驟5 - 再次從1到num-1執行外部for迴圈
步驟6 - 從1到num-m執行巢狀for迴圈。此迴圈用於列印空格,並且在每次迭代中空格減少一個。
步驟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
在上面的程式碼中,我們使用帶有條件x>=1的while迴圈。此迴圈使用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 // Length of the triangle pattern var num = 6 // Creating upper half portion // Handle length of the upper half portion 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
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP