使用二进制运算查找最大和最小数的 Go 语言程序。
示例
例如,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\n", x, y, x - ((x - y) &
((x - y) >> 31)))
}
func FindMin(x, y int) {
fmt.Printf("Minimum element in %d, and %d is: %d\n", x, y, y + ((x - y) &
((x - y) >> 31)))
}
func main(){
FindMax(12, 15)
FindMin(13, 17)
FindMax(1, 0)
}输出
Maximum element in 12, and 15 is: 15 Minimum element in 13, and 17 is: 13 Maximum element in 1, and 0 is: 1
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP