编写一个 Golang 程序,在不使用第三个变量的情况下交换两个数字
解决这个问题的方法
第一步:定义一个接受两个数字的函数,类型为int。
第二步:求b=a+b;
第3步:然后a=b–a和b=b–a
程序
package main
import "fmt"
func swap(a, b int){
fmt.Printf("Before swapping, numbers are %d and %d\n", a, b)
b = a + b
a = b - a
b = b - a
fmt.Printf("After swapping, numbers are %d and %d\n", a, b)
}
func main(){
swap(23, 45)
swap(56, 100)
}输出结果Before swapping, numbers are 23 and 45 After swapping, numbers are 45 and 23 Before swapping, numbers are 56 and 100 After swapping, numbers are 100 and 56
热门推荐
10 读书祝福语女生短句简短
11 感谢同事新婚祝福语简短
12 感谢教官文案祝福语简短
13 员工聚会蛋糕祝福语简短
14 元宵发客户祝福语简短
15 给团队信心祝福语简短
16 医美的祝福语大全简短
17 异国姻缘的祝福语简短
18 兄妹拍照祝福语简短的话