如何在 Golang 中求矩形的周長?
在本教程中,我們將瞭解如何使用 Golang 程式來求矩形的周長。周長是任何封閉圖形邊界線的總長度。
公式
Perimeter of rectangle - 2 * ( L + B ) L - Length of a rectangle B - Breadth of a rectangle
例如,一個矩形的長為 100 釐米,寬為 50 釐米,那麼矩形的周長為:
Perimeter = 2 * (100 + 50) cm = 2 * (150) cm = 300 cm
在函式中求圓的周長
演算法
步驟 1 - 宣告長度、寬度和周長變數,資料型別為 float64。
步驟 2 - 從使用者獲取長度和寬度的輸入。
步驟 3 - 使用上述公式在函式中求周長。
步驟 4 - 列印結果。
時間複雜度
O(1) - 時間複雜度為常數,因為無論輸入是什麼,程式都會花費相同的時間。
空間複雜度
O(1) - 程式中的變數是靜態的,因此空間複雜度也是常數。
示例 1
在本例中,我們將瞭解如何在函式中求矩形的周長。
package main // fmt package provides the function to print anything import ( "fmt" ) func main() { // declaring the floating variables using the var keyword for // storing the length and breadth also a variable to store Perimeter var length, breadth, Perimeter float64 fmt.Println("Program to find the perimeter of a rectangle.") // initializing the length of a rectangle length = 30 // initializing the breadth of a rectangle breadth = 10.4 // finding the Perimeter of a rectangle Perimeter = 2.0 * (length + breadth) // printing the result fmt.Println("Length = ", length, "\nBreath =", breadth, "\nPreimeter of the Rectangle = ", Perimeter, "cm.") fmt.Println("(Finding the Perimeter of a circle within the function)") }
輸出
Program to find the perimeter of a rectangle. Length = 30 Breath = 10.4 Preimeter of the Rectangle = 80.8 cm. (Finding the Perimeter of a circle within the function)
程式碼描述
var length, breadth, Perimeter float64 - 在這行程式碼中,我們聲明瞭稍後將使用的長度、寬度和周長。由於長度、寬度或周長可能為小數,因此我們使用了 float 資料型別。
Perimeter = 2.0 * (length + breadth) - 在這行程式碼中,我們應用公式並求周長。
fmt.Println("The perimeter of a rectangle whose length is", length, "and the breadth is", breadth, "is", Perimeter, "cm.") - 列印矩形的周長。
在不同函式中求圓的周長
演算法
步驟 1 - 宣告長度、寬度和周長變數,資料型別為 float64。
步驟 2 - 從使用者獲取長度和寬度的輸入。
步驟 3 - 呼叫函式並將長度和寬度作為引數,並將函式返回的周長儲存起來
步驟 4 - 列印結果。
示例 2
在本例中,我們將瞭解如何透過定義單獨的函式來求矩形的周長。
package main // fmt package provides the function to print anything import ( "fmt" ) func perimeterOfRectangle(length, breadth float64) float64 { // returning the perimeter of a rectangle using the formula return 2 * (length + breadth) } func main() { // declaring the floating variables using the var keyword for // storing the length and breadth also a variable to store parameter var length, breadth, perimeter float64 fmt.Println("Program to find the perimeter of a rectangle.") // taking the length of a rectangle as input from the user fmt.Print("Please enter the length of a rectangle:") fmt.Scanln(&length) // taking the breadth of a rectangle as input from the user fmt.Print("Please enter the breadth of a rectangle: ") fmt.Scanln(&breadth) // calling the perimeter of rectangle function by passing the respective parameter // and storing the result perimeter = perimeterOfRectangle(length, breadth) // printing the result fmt.Println("The perimeter of a rectangle whose length is", length, "and the breadth is", breadth, "is", perimeter, "cm.") fmt.Println("(Finding the perimeter of a circle in different function)") }
輸出
Program to find the perimeter of a rectangle. Please enter the length of a rectangle:20.5 Please enter the breadth of a rectangle: 10 The parameter of a rectangle whose length is 20.5 and the breadth is 10 is 61 cm. (Finding the perimeter of a circle in different function)
程式碼描述
var length, breadth, Perimeter float64 - 在這行程式碼中,我們聲明瞭稍後將使用的長度、寬度和周長。由於長度、寬度或周長可能為小數,因此我們使用了 float 資料型別。
fmt.Scanln(&length) - 從使用者獲取長度的輸入。
fmt.Scanln(&breadth) - 從使用者獲取寬度的輸入。
perimeter = perimeterOfRectangle(length, breadth) - 在這行程式碼中,我們呼叫了用於求矩形周長的函式。
fmt.Println("The perimeter of a rectangle whose length is", length, "and the breadth is", breadth, "is", Perimeter, "cm.") - 列印矩形的周長。
結論
以上是在 Golang 中求矩形周長的兩種方法。從模組化和程式碼可重用性的角度來看,第二種方法更好,因為我們可以在專案的任何地方呼叫該函式。要了解更多關於 go 的資訊,您可以探索這些 教程。