Created
June 8, 2013 03:25
-
-
Save nida-001/5733885 to your computer and use it in GitHub Desktop.
「最強最速アルゴリズマー」のキウイジュースの問題をGoで写経してみた
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" | |
) | |
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