Skip to content

Instantly share code, notes, and snippets.

@nida-001
Created June 8, 2013 03:25
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 nida-001/5733885 to your computer and use it in GitHub Desktop.
Save nida-001/5733885 to your computer and use it in GitHub Desktop.
「最強最速アルゴリズマー」のキウイジュースの問題をGoで写経してみた
package main
import (
"fmt"
)
func main() {
capacities := []int{20, 20}
bottles := []int{5,8}
fromId := []int{0}
toId := []int{1}
s := thePouring(capacities, bottles, fromId, toId)
fmt.Println(s)
}
func thePouring(capacities, bottles, fromId, toId []int) []int {
for i := 0; i < len(fromId); i++ {
f := fromId[i]
t := toId[i]
space := capacities[t] - bottles[t]
if (space >= bottles[f]) {
vol := bottles[f]
bottles[t] += vol
bottles[f] = 0
} else {
vol := space
bottles[t] += vol
bottles[f] -= vol
}
}
return bottles[:len(bottles)]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment