Skip to content

Instantly share code, notes, and snippets.

@velppa
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velppa/8a1b11532a0925df7b8f to your computer and use it in GitHub Desktop.
Save velppa/8a1b11532a0925df7b8f to your computer and use it in GitHub Desktop.
Problems of blog.svpino.com
package main
import "fmt"
import "strconv"
import "math"
//maxInt takes []int and returns maximum integer stacking
//them in any order
func maxInt(nums []int) int {
if len(nums) == 0 {
return 0
}
// get str length of comibining all numbers as strings
var numLen int
for _, n := range nums {
numLen += len(strconv.Itoa(n))
//fmt.Println(strconv.Itoa(n))
}
//fmt.Println(numLen)
// get max number of lenght numLen
var maxNum, idx, curNum int
for i, n := range nums {
curNum = n*int(math.Pow10(numLen-len(strconv.Itoa(n))))
if curNum > maxNum {
maxNum = curNum
idx = i
}
}
//fmt.Println(maxNum)
nums[0], nums[idx] = nums[idx], nums[0]
return maxNum + maxInt(nums[1:])
}
func main() {
//fmt.Println(strconv.Itoa(40))
nums := []int{50,2,1,9}
fmt.Println("Input numbers:", nums)
maxint := maxInt(nums)
fmt.Println("Max Int = ", maxint)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment