1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// @Title: 复数乘法 (Complex Number Multiplication)
// @Author: 15816537946@163.com
// @Date: 2022-02-25 17:56:28
// @Runtime: 0 ms
// @Memory: 1.8 MB
import "strings"

func complexNumberMultiply(num1 string, num2 string) string {
	a := strings.Split(num1, "+")
	b := strings.Split(num2, "+")

	a1,_ := strconv.Atoi(a[0])
	a2, _ := strconv.Atoi(strings.ReplaceAll(a[1],"i",""))

	b1,_ := strconv.Atoi(b[0])
	b2, _ := strconv.Atoi(strings.ReplaceAll(b[1],"i",""))

	// a1*b1 + a1*b2 + a2 *b1 + a2*b2
	// a1*b1 结果为正数
	// a2*b2 结果为负数
	// a1*b2 + a2*b1 结果为虚数
	ret1 := a1 *b1 -(a2*b2)
	ret2 := a1*b2 +a2*b1
	return  fmt.Sprintf("%d+%di", ret1,ret2)

}