Created
August 28, 2017 15:40
-
-
Save luojiyin1987/5275d50537c9fdb94eec759713e8f260 to your computer and use it in GitHub Desktop.
Base 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func convertToBase7(num int) string { | |
| if num == 0 { | |
| return "0" | |
| } | |
| var positive bool | |
| var result string | |
| if num < 0 { | |
| num = -num | |
| positive = true | |
| } | |
| for num !=0 { | |
| result = strconv.Itoa(num%7) + result | |
| num /= 7 | |
| } | |
| if positive { | |
| result = "-" + result | |
| } | |
| return result | |
| } | |
| //---------------------------- | |
| func convertToBase7(num int) string { | |
| if num == 0 { | |
| return "0" | |
| } | |
| var neg bool | |
| if num < 0 { | |
| neg = true | |
| num *= -1 | |
| } | |
| res := "" | |
| for ; num != 0; num /= 7 { | |
| res = strconv.Itoa(num%7) + res | |
| } | |
| if neg { | |
| res = "-" + res | |
| } | |
| return res | |
| } | |
| //---------------------- | |
| func convertToBase7(num int) string { | |
| if num == 0 { | |
| return "0" | |
| } | |
| var res, sign string | |
| if num<0 { | |
| sign = "-" | |
| num=-num | |
| } | |
| for ;num>0; num/=7 { | |
| res = string((byte(num%7)+'0'))+res | |
| } | |
| return sign+res | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment