Golang 如何找到矩形的周长

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中查找矩形周长的两种方法。第二种方法在模块化和代码重用性方面要好得多,因为我们可以在项目的任何地方调用该函数。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程