N
N
nolouds2021-12-23 19:02:25
go
nolouds, 2021-12-23 19:02:25

How to output data after input using recursion (without loops and goto)?

The code asks for the number of inputs, then on each input for the number of numbers, then asks for the numbers themselves and computes the sum of their squares. But the program does not work the way I wanted. Now she gives the answer in the process of entering data. That is, there should be 3 inputs of numbers and 3 answers, I enter the data for the first answer and the program immediately, as soon as it receives all the data, issues an answer and waits for the data for the next answer. And I need the program to give answers in turn after all the inputs, an example of how I want:

Input:

2 // Количество вводов, столько же ответов должно быть
3 // Количество чисел в первом вводе
1 2 3 // сами числа, программа должны вычислить сумму их квадратов
2 // кол-о чисел во втором вводе
2 3 // сами числа


output:
14 // ответ на первый ввод
13 // ответ на второй ввод


The program itself:
package main

import (
  "bufio"
  "fmt"
  "os"
  "strconv"
  "strings"
)

func main() {
  reader := bufio.NewReader(os.Stdin)
  fmt.Println("input number")
  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) {
  if test_cases != 0 {
    fmt.Println("int number")
    _, _ = reader.ReadString('\n')
    fmt.Println("integers")
    input, _:= reader.ReadString('\n')
    input = strings.TrimRight(input, "\r\n")
    fmt.Println(input)
    arr := strings.Split(input,  " ")
    ans := process_array(arr, 0)
    fmt.Println("answer =", ans)
    test_cases -= 1
    process_test_case(test_cases, reader)
  }
}

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
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
uvelichitel, 2021-12-24
@nolouds

It is possible without cycles and without slice allocation. It will only print backwards.

func process_test_case(test_cases int, reader *bufio.Reader) {
  fmt.Println("int number")
  _, _ = reader.ReadString('\n')
  fmt.Println("integers")
  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("answer =", process_array(arr, 0))
}

V
Vasily Demin, 2021-12-24
@includedlibrary

No cycles whatsoever. You need to store the answers in an array, and then display them one by one. For this, in fact, a cycle will be needed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question