Last active
August 29, 2015 14:20
-
-
Save velppa/8a1b11532a0925df7b8f to your computer and use it in GitHub Desktop.
Problems of blog.svpino.com
This file contains 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
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