package main
import (
"fmt"
"strconv"
)
func main() {
var input string
for {
fmt.Println("")
fmt.Println("请输入2的几次幂:")
if _, err := fmt.Scanf("%s", &input); err != nil {
fmt.Printf("%s\n", err)
return
}
if input == "exit" {
fmt.Println("退出成功~")
return
} else {
ret := calculate( input )
fmt.Println("结果为:")
for i := len(ret)-1;i>=0 ;i-- {
fmt.Print(ret[i])
}
fmt.Println()
}
fmt.Println("输入【exit】程序退出~")
}
}
func calculate(input string) []int {
ret :=[]int{1}
power,_ := strconv.Atoi(input)
if power<=0{
return ret
}
carry :=0
for i:=0;i< power ;i++ {
for j:=0;j<len(ret);j++ {
tmp := (2 * ret[j]) + carry
if tmp>=10 {
ret[j] = tmp%10
carry = 1
}else{
ret[j] = tmp
carry = 0
}
}
if carry>0 {
ret = append(ret,carry)
carry=0
}
}
return ret
}
网友评论