question
You have 50 gold coins that you need to allocate to: Matthew, Sarah, Augustus, Heidi, Emilie, Peter, Giana, Adriano, Aaron, Elizabeth.
The allocation rules are as follows:
a. Each name contains 'e' or 'E' and is divided into 1 gold coin
b. Each name contains 'i' or 'I' and 2 gold coins
c. Each name contains 'o' or 'O' and 3 gold coins
d: Each name contains 'u' or 'U' and is divided into 4 gold coins
Write a program to calculate how many gold coins each user gets and how many gold coins remain in the end?
The program structure is as follows, please implement the ‘dispatchCoin’ function
Code implementation
package main import ( "fmt" "strings" ) var ( coins = 50 users = []string{ "Matthew", "Sarah", "Augustus", "Heidi", "Emilie", "Peter", "Giana", "Adriano", "Aaron", "Elizabeth", } distribution = make(map[string]int, len(users)) ) // Determine whether string s contains any character in string chars func dispatchCoin() int { for _, i := range users { if (i, "e & E") { distribution[i]++ } if (i, "i & I") { distribution[i] += 2 } if (i, "o & O") { distribution[i] += 3 } if (i, "u & U") { distribution[i] += 4 } coins -= distribution[i] } return coins } func main() { left := dispatchCoin() for _, i := range users { (i, distribution[i]) } ("remain:", left) }
Running effect
D:\Software\godata\src\1201>go run
Matthew 1
Sarah 0
Augustus 4
Heidi 3
Emilie 3
Peter 1
Giana 2
Adriano 5
Aaron 3
Elizabeth 3
Left: 25
This is the end of this article about implementing the gold coin game based on Go language. For more related content of Go gold coin game, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!