如何在 Golang 中計算商和餘數?


在本教程中,我們將瞭解透過對使用者提供的被除數和除數進行算術運算來查詢商和餘數的方法。本教程將介紹兩種實現相同目標的方法。

解釋

要除任何數字,我們使用“ / ”算術運算子,要查詢餘數,我們使用“ % ”算術運算子

假設我們有一個被除數 19 和一個除數 4,則商和餘數如下

Quotient = dividend / divisor
   = 19 / 4
   = 4 
Remainder = dividend % divisor
   = 19 % 4
   = 3

函式內的商和餘數

演算法

  • 步驟 1 − 宣告 int32 資料型別的被除數、除數、商和餘數變數。
  • 步驟 2 − 從使用者處獲取被除數和除數的輸入。
  • 步驟 3 − 使用上述算術運算子在函式內查詢商和餘數。
  • 步驟 4 − 列印結果。

時間複雜度

O(1) - 時間複雜度是常數,因為無論輸入是什麼,程式都將花費相同的時間。

空間複雜度

O(1) - 變數在程式中是靜態的,因此空間複雜度也是常數。

示例 1

在此示例中,我們將查詢函式內的商和餘數。

package main // fmt package provides the function to print anything import ( "fmt" ) func main() { // declaring the variables to store dividend, divisor, // quotient, and the remainder of type int32 using the var keyword var dividend, divisor, quotient, remainder int32 fmt.Println("Program to find the quotient and the remainder of a number within the function.") // initializing the dividend dividend = 92 // initializing the dividend divisor = 7 // finding the quotient by / arithmetic operator quotient = dividend / divisor // finding the remainder by % arithmetic operator remainder = dividend % divisor // Printing the result fmt.Println("Dividend =", dividend, "\nDivisor=", divisor) fmt.Println("Quotient = ", quotient) fmt.Println("Reminder = ", remainder) }

輸出

Program to find the quotient and the remainder of a number within the function.
Dividend = 92
Divisor= 7
Quotient = 13
Reminder = 1

程式碼描述

  • var dividend, divisor, quotient, remainder int32 − 在此行中,我們聲明瞭稍後將使用的被除數、除數、商和餘數。由於被除數、除數、商和餘數將為整數,因此我們使用了 int32 資料型別。

  • quotient = dividend / divisor − 使用 / 算術運算子查詢商

  • remainder = dividend % divisor − 使用 % 算術運算子查詢餘數。

  • fmt.Println("The quotient is", quotient) − 列印商

  • fmt.Println("The remainder is", remainder) − 列印餘數。

單獨函式中的商和餘數。

演算法

  • 步驟 1 − 宣告 int32 資料型別的被除數、除數、商和餘數變數。

  • 步驟 2 − 從使用者處獲取被除數和除數的輸入

  • 步驟 3 − 使用被除數和除數作為引數呼叫函式,並存儲 quotientFunction(dividend, divisor int32) 函式返回的商。

  • 步驟 4 − 使用長度和寬度作為引數呼叫函式,並存儲 remainderFunction(dividend, divisor int32) 函式返回的餘數

  • 步驟 5 − 列印結果。

示例 2

在此示例中,我們將透過定義單獨的函式來查詢商和餘數。

package main // fmt package provides the function to print anything import ( "fmt" ) // This function is returning the quotient func quotientFunction(dividend, divisor int32) int32 { // finding the quotient by / arithmetic operator return dividend / divisor } // This function is returning the remainder func remainderFunction(dividend, divisor int32) int32 { // finding the remainder by % arithmetic operator return dividend % divisor } func main() { // declaring the variables to store dividend, divisor, // quotient, and the remainder of type int32 using the var keyword var dividend, divisor, quotient, remainder int32 fmt.Println("Program to find the quotient and the remainder of a number by using the separate function.") // scanning the value of the dividend from the user fmt.Println("Enter the value of the dividend:") fmt.Scanln(÷nd) // scanning the value of the divisor from the user fmt.Println("Enter the value of the divisor:") fmt.Scanln(&divisor) // finding the quotient by calling the quotientFunction() function quotient = quotientFunction(dividend, divisor) // finding the remainder by calling the remainderFunction() function remainder = remainderFunction(dividend, divisor) // Printing the result fmt.Println("The quotient is", quotient) fmt.Println("The remainder is", remainder) }

輸出

Program to find the quotient and the remainder of a number by using the separate function.
Enter the value of the dividend:
63
Enter the value of the divisor:
4
The quotient is 15
The remainder is 3

程式碼描述

  • var dividend, divisor, quotient, remainder int32 − 在此行中,我們聲明瞭稍後將使用的被除數、除數、商和餘數。由於被除數、除數、商和餘數將為整數,因此我們使用了 int32 資料型別。

  • fmt.Scanln(÷nd) − 從使用者處獲取被除數的輸入。

  • fmt.Scanln(&divisor) − 從使用者處獲取除數的輸入。

  • quotient = quotientFunction(dividend, divisor) − 透過呼叫 quotientFunction() 函式查詢商。

    • func quoitentFunction(dividend, divisor int32) int32 {} − 這是包含查詢商邏輯的函式。該函式具有 int32 型別的被除數和除數作為引數,並且還具有 int32 的返回型別。

    • func remainderFunction(dividend, divisor int32) int32 {} − 這是包含查詢餘數邏輯的函式。該函式具有 int32 型別的被除數和除數作為引數,並且還具有 int32 的返回型別。

  • remainder = remainderFunction(dividend, divisor) − 透過呼叫 remainderFunction() 函式查詢商。

  • fmt.Println("The quotient is", quotient) − 列印商。

  • fmt.Println("The remainder is", remainder) − 列印餘數。

結論

這是在 Golang 中查詢商和餘數的兩種方法。從模組化和程式碼可重用性的角度來看,第二種方法更好,因為我們可以在專案的任何地方呼叫該函式。要了解有關 go 的更多資訊,您可以瀏覽這些 教程

更新於: 2022 年 8 月 29 日

1K+ 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告