题解 | 十六进制转十进制

十六进制转十进制

https://www.nowcoder.com/practice/33e148570d5c4e728116e2f861638c9c

package main

import (
	"fmt"
	"math"
	"strings"
)

func main() {
    var a string = "ABCDEF"
    var sum int = 0
    var temp int = 0
    temps := strings.Split(a, "")
    for i:=0;i<len(temps);i++{
        if temps[i] ==string('A'){
             temp  = 10
        }else if temps[i] ==string('B'){
             temp  = 11
        }else if temps[i] ==string('C'){
             temp  = 12
        }else if temps[i] == string('D'){
             temp  = 13
        }else if temps[i] ==string('E'){
             temp  = 14
        }else if temps[i] ==string('F'){
             temp  = 15
        }else{
            fmt.Print("输入内容有误")
        }
        var b=math.Pow(16, float64(len(temps)-i-1))
        sum = temp*int(b)+sum
    }
    fmt.Printf("%15d",sum)
}

全部评论

相关推荐

每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
06-05 19:46
已编辑
武汉大学 后端
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务