0
点赞
收藏
分享

微信扫一扫

每日一题 --- 537. 复数乘法[力扣][Go]

先峰老师 2022-02-25 阅读 81

题目:

解题代码:

func complexNumberMultiply(num1 string, num2 string) string {
	// 先获取实部数值
	var a1,a2,b1,b2 int
	f := func(c rune) bool {
		if c == '+' || c == 'i'{
			return true
		} else {
			return false
		}
	}
	one := strings.FieldsFunc(num1, f)
	two := strings.FieldsFunc(num2, f)
	a1,_ = strconv.Atoi(one[0])
	a2,_ = strconv.Atoi(one[1])
	b1,_ = strconv.Atoi(two[0])
	b2,_ = strconv.Atoi(two[1])
	// 计算
	aa := a1 * b1 - a2 * b2
	bb := a1 * b2 + a2 * b1
	return strconv.Itoa(aa) + "+" + strconv.Itoa(bb) + "i"
}

在这里插入图片描述

举报

相关推荐

0 条评论