找到 1082 篇文章 關於 Go 程式設計
181 次瀏覽
示例例如,x = 12,y = 15 => 最大數是 15。例如,x = 13,y = 17 => 最小數是 13。解決此問題的方法步驟 1 - 定義方法 findMax 和 findMin,它們接受兩個整數 x 和 y。步驟 2 - 根據定義的方法返回整數。示例 線上演示package main import "fmt" func FindMax(x, y int){ fmt.Printf("Maximum element in %d, and %d is: %d", x, y, x - ((x - y) & ((x - y) >> 31))) } func FindMin(x, y int) { fmt.Printf("Minimum element in %d, and %d is: %d", x, y, y ... 閱讀更多
2K+ 次瀏覽
示例例如,101、11、11011 和 1001001 設定的位數分別為 2、2、4 和 3。解決此問題的方法步驟 1 - 將數字轉換為二進位制表示形式。步驟 2 - 計數 1 的個數;返回計數。示例 線上演示package main import ( "fmt" "strconv" ) func NumOfSetBits(n int) int{ count := 0 for n !=0{ count += n &1 n >>= 1 } return count } func main(){ n := 20 fmt.Printf("Binary representation of %d is: %s.", n, strconv.FormatInt(int64(n), 2)) fmt.Printf("The total number of set bits in %d is %d.", n, NumOfSetBits(n)) }輸出Binary representation of 20 is: 10100. The total number of set bits in 20 is 2.
197 次瀏覽
示例例如,101、11、11011、1001001 是迴文。100、10010 不是迴文。解決此問題的方法步驟 1 - 將數字轉換為二進位制表示形式。步驟 2 - 從兩側遍歷轉換後的二進位制表示形式,並檢查表示形式是否為迴文。示例 線上演示package main import ( "fmt" "strconv" ) func IsPalindrome(n int) bool{ rev := 0 k := n for k != 0 { rev = (rev > 1 } return n == rev } func main(){ n := 3 fmt.Printf("Binary representation of %d is: %s.", n, strconv.FormatInt(int64(n), 2)) if IsPalindrome(n) == true{ fmt.Println("Palindrome") } else { fmt.Println("Not a Palindrome") } }輸出Binary representation of 3 is: 11. Palindrome
102 次瀏覽
示例考慮兩個數字 m = 65 => 01000001 和 n = 80 => 01010000翻轉的位數為 2。解決此問題的方法步驟 1 - 將兩個數字都轉換為位。步驟 2 - 統計翻轉的位數。示例 線上演示package main import ( "fmt" "strconv" ) func FindBits(x, y int) int{ n := x ^ y count := 0 for ;n!=0; count++{ n = n & (n-1) } return count } func main(){ x := 65 y := 80 fmt.Printf("Binary of %d is: %s.", x, strconv.FormatInt(int64(x), 2)) ... 閱讀更多
759 次瀏覽
定義 - 奇偶校驗指的是 1 的個數。如果 1 的個數為偶數,則為偶校驗;如果 1 的個數為奇數,則奇偶校驗為奇數。示例考慮 n = 20(00010100)給定數字 20 的奇偶校驗為偶數。解決此問題的方法步驟 1 - 定義一個方法,其中 n 是引數,返回型別為 int。步驟 2 - 計算給定數字位中 1 的個數。示例 線上演示package main import ( "fmt" "strconv" ) func FindParity(n int) bool { parity := false for n != 0 { ... 閱讀更多
263 次瀏覽
示例考慮 n = 20(00010100)現在返回 log2(20 & -20) => 2+1 => 3解決此問題的方法步驟 1 - 定義一個方法,其中 n 是引數,返回型別為 int。步驟 2 - 返回 log2(n & -n)+1。示例package main import ( "fmt" "math" "strconv" ) func FindRightMostSetBit(n int) int { if (n & 1) != 0{ return 1 } return int(math.Log2(float64(n & -n))) + 1 } func main(){ var n = 20 fmt.Printf("Binary of %d is: %s.", n, strconv.FormatInt(int64(n), 2)) fmt.Printf("Position of the rightmost set bit of the given number %d is %d.", n, FindRightMostSetBit(n)) }輸出Binary of 20 is: 10100. Position of the rightmost set bit of the given number 20 is 3.
584 次瀏覽
示例考慮 n = 16(00010000)現在找到 x = n-1 => 15(00001111) => x & n => 0解決此問題的方法步驟 1 - 定義一個方法,其中 n 是引數,返回型別為 int。步驟 2 - 執行 x = n & n-1。步驟 3 - 如果 x 為 0,則給定數字為 2 的冪;否則不是。示例 線上演示package main import ( "fmt" "strconv" ) func CheckNumberPowerOfTwo(n int) int { return n & (n-1) } func main(){ var n = 16 fmt.Printf("Binary of %d is: %s.", n, strconv.FormatInt(int64(n), 2)) flag := CheckNumberPowerOfTwo(n) ... 閱讀更多
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP