程式應該詢問每個“輸入數量”的“數字數量”和“數字”值,答案是這些數字的平方和。我的代碼有效,但它以錯誤的順序顯示答案,如何使其正常作業?
我認為通過閱讀輸入和輸出更容易理解這個程式:
Enter the number of inputs // output
2 // input
Enter the number of numbers // output
2 // input
Enter the numbers // output
1 2 // input (second ans)
Enter the number of numbers // output
2 // input
Enter the numbers
2 3 // input (first ans)
ans = 13 // ans = 2^2 3^2
ans = 5 () // ans = 1^2 2^2
我的代碼:
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Println(`Enter the number of inputs`)
n, _ := reader.ReadString('\n')
n = strings.TrimRight(n, "\r\n")
test_cases, err := strconv.Atoi(n)
if err != nil {
fmt.Println(err)
}
process_test_case(test_cases, reader)
}
func process_test_case(test_cases int, reader *bufio.Reader) {
fmt.Println(`Enter the number of numbers`)
_, _ = reader.ReadString('\n')
fmt.Println(`Enter the numbers`)
input, _ := reader.ReadString('\n')
input = strings.TrimRight(input, "\r\n")
arr := strings.Split(input, " ")
test_cases -= 1
if test_cases != 0 {
process_test_case(test_cases, reader)
}
fmt.Println("ans = ", process_array(arr, 0))
}
func process_array(arr []string, result int) int {
num, _ := strconv.Atoi(arr[0])
if len(arr) > 1 {
next := arr[1:]
if num < 0 {
num = 0
}
result = num*num process_array(next, result)
return result
} else {
if num >= 0 {
return num * num
}
return 0
}
}
uj5u.com熱心網友回復:
我為您的場景創建了示例代碼。您可以通過使用修改它bufio.NewReader(os.Stdin)
func process_array(arr []string) int {
res := 0
for _, v := range arr {
num, err := strconv.Atoi(v)
if err != nil {
panic(err)
}
fmt.Println("num :", num)
res = num * num
}
return res
}
func process_test_case() int {
fmt.Println(`Enter the number of numbers`)
num := 2
fmt.Println("number of numbers :", num)
fmt.Println(`Enter the numbers`)
input := "1 2"
fmt.Println("the numbers :", input)
arr := strings.Split(input, " ")
res := process_array(arr)
return res
}
func main() {
fmt.Println(`Enter the number of inputs`)
test_cases := 1
fmt.Println("number of inputs :", test_cases)
for test_cases >= 1 {
res := process_test_case()
fmt.Println(res)
test_cases -= 1
}
}
你可以在這里運行它:https : //go.dev/play/p/zGkAln2ghZp
或者
正如@phonaputer 所評論的,您可以更改順序。回傳切片并從末尾列印。
uj5u.com熱心網友回復:
我認為這段代碼回答了你的問題標題:
package main
import "fmt"
func SumValues(x int, y ...int) (sum int) {
q := len(y) - 1
sum = y[q - x]
if x < q {
sum = SumValues(x 1, y...)
}
return sum
}
func main() {
sum := SumValues(0,1,2,3,4,5)
fmt.Println("Sum is:", sum)
}
uj5u.com熱心網友回復:
如何在 Go 中使用遞回(無回圈)求整數和?
程式應該詢問每個“輸入數量”的“數字數量”和“數字”,答案是這些數字的平方和。
這是問題的答案,Go 中的遞回解決方案:
$ go run sumsq.go
Enter the number of inputs:
2
Enter the number of numbers:
2
Enter the numbers:
1
2
Sum of Squares: 5
Enter the number of numbers:
2
Enter the numbers:
2
3
Sum of Squares: 13
$
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func readInt(r *bufio.Reader) int {
line, err := r.ReadString('\n')
line = strings.TrimSpace(line)
if err != nil {
if len(line) == 0 {
return 0
}
}
i, err := strconv.Atoi(line)
if err != nil {
return 0
}
return i
}
func nSquares(n int, r *bufio.Reader) int {
if n == 0 {
return 0
}
i := readInt(r)
return i*i nSquares(n-1, r)
}
func nNumbers(n int, r *bufio.Reader) int {
if n == 0 {
return 0
}
fmt.Println("\nEnter the number of numbers: ")
i := readInt(r)
fmt.Println("Enter the numbers: ")
sumSquares := nSquares(i, r)
fmt.Println("Sum of Squares:", sumSquares)
return nNumbers(n-1, r)
}
func nInputs(r *bufio.Reader) int {
fmt.Println("Enter the number of inputs: ")
i := readInt(r)
return nNumbers(i, r)
}
func main() {
r := bufio.NewReader(os.Stdin)
fmt.Println()
nInputs(r)
fmt.Println()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/404488.html
標籤:
