36进制数由 0~9 , a~z 共 36 个字符组成,请你按照加法规则计算两个 36 进制数的和。
例如 1b+2b = 3m
数据范围:两个数的长度都满足 , 两个数中包含的字符仅包括 和
package main func thirtysixAdd( A string , B string ) string { lA, lB := len(A), len(B) res := []byte{} // 转为数字进行计算 i, j, carry := lA-1, lB-1, 0 for i >= 0 || j >= 0 || carry > 0 { t := 0 if i >= 0 { t = count(A[i]) i-- } if j >= 0 { t += count(B[j]) j-- } t += carry res = append(res, byte(t%36)) carry = t / 36 } // 反转 for i := 0; i < len(res) / 2; i++ { res[i], res[len(res)-i-1] = res[len(res)-i-1], res[i] } // 转回字母返回答案 for i, v := range res { if v < 10 { res[i] = v + '0' } else { res[i] = 'a' + v - 10 } } return string(res) } func count(a byte) int { if a > '9' { return int(a-'a'+10) } else { return int(a-'0') } }