题解 | 进制转换

进制转换

https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strings"
)

// 得到输入内容
func getPrintStr() (str string) {
    reader := bufio.NewReader(os.Stdin)
    str, _ = reader.ReadString('\n')
    str = strings.TrimSuffix(str, "\n")
    str = str[2:]
    // fmt.Println(str)
    return
}

func getResult(str string) {
    result := 0
    for i := 0; i < len(str); i++ {
        value := 0
        if str[i] >= '0' && str[i] <= '9' {
            value = int(str[i] - '0')
        } else {
            value = int(str[i] - 'A' + 10)
        }

        result += value*int(math.Pow(16, float64(len(str) - i - 1)))
        // positionWeight := 1
        // for j := 0; j < len(str) - i - 1; j ++ {
        //     positionWeight *= 16
        // }
        // result += value*positionWeight
    }
    fmt.Println(result)
}

func main() {
    str := getPrintStr()
    getResult(str)
}

#算法题解#
全部评论

相关推荐

在考古的丘比特很冷艳:[牛泪我横向一周多自动挂了,面试答的还行,感觉挂的莫名其妙
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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