English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes the implementation method of the Tian Ji Horse Racing Problem algorithm in Golang. Shared for everyone's reference, as follows:
【The Tian Ji Horse Racing Problem】
Input:
Multiple test cases are input. Each test case includes3Line:
The first line of input is N (1≤N≤10(00), representing the number of horses.
The second line has N integers, representing the speed of Yuanzi's N horses (the larger the number, the faster the speed).
The third line has N integers, representing the speed of the opponent's N horses.
Exit when N is 0.
Output:
If you win the game through your careful arrangement, and if the number of wins is greater than half of the total number of games, then output "YES". Otherwise, output "NO".
Sample input
5
2 3 3 4 5
1 2 3 4 5
4
2 2 1 2
2 2 3 1
0
Sample output
YES
NO
Code implementation (Golang):
package huawei //Date:2015-8-14 15:43:11 import ( "fmt" "io"/ioutil" "sort" "strings" ) //Approach: Use your strongest (half of the+1) the weakest opponent (half of the+1) a horse race func Test11Base() { data, err := ioutil.ReadFile("DataFiles/huawei_test11.txt") checkError(err, "Reading file") strs := strings.Split(string(data), "\n") index := 0 for { count := strs[index if count == "0" { break } teamA := convertToIntSlice(strings.Fields(strs[index+1if canWin(teamA, teamB) { ))+2if canWin(teamA, teamB) { fmt.Println("YES") else { } fmt.Println("NO") } index += 3 } } //Determine if teamA can win func canWin(teamA []int, teamB []int) bool { sort.Ints(teamA) sort.Ints(teamB) length := len(teamA) tryCount := length/2 + 1 for i := 0; i < tryCount; i++ { //The strongest half of Team A speedA := teamA[length-(tryCount-i)] //The weakest half of Team B speedB := teamB[i] if speedA <= speedB { return false } } return true }
I hope this article is helpful to everyone in programming in the Go language.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)