简体   繁体   English

如何创建n个数字的平方和的递归方法?

[英]How to create a recursive sum of squares method of n numbers?

My code must recive n numbers and return the sum of their squares in a recursive method. 我的代码必须接收n个数字,并以递归方法返回其平方和。

Ex: the sum of 1,1,2,2,3 squares must be 19 (1+1+4+4+9) 例如:1、2、2、3平方和必须为19(1 + 1 + 4 + 4 + 9)

My code is printing 14 for some reason. 由于某种原因,我的代码正在打印14。

import java.util.Scanner;

/**
 *
 * @author User
 */
public class SomaQuadrados {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
      Scanner teclado = new Scanner(System.in);
      int n = teclado.nextInt();
      int numero = 0;
      for(int i = 0; i < n; i++)
      {
           numero = teclado.nextInt();
      }

      System.out.println(somaQuadrados(numero));

    }

    public static int somaQuadrados(int numero)
    {
        if(numero == 0)
        {
            return 0;
        }
        else
        {
            return somaQuadrados(numero-1) + (numero*numero);
        }
    }

}

The problem is your recursive function somaQuadrados() should be processing the list [1, 1, 2, 2, 3] of numbers. 问题在于您的递归函数somaQuadrados()应该正在处理数字的列表[1, 1, 2, 2, 3] somaQuadrados() [1, 1, 2, 2, 3] That is: 那是:

<first number> * <first number> + somaQuadrados(<rest of numbers>)

with base case of an empty list. 与空列表的基本情况。 But instead you're taking an individual number and calculating: 但取而代之的是,您使用一个单独的数字并计算:

<number> * <number> + somaQuadrados(<number - 1>)

Which is a completely different program. 这是一个完全不同的程序。 When you process the final element 3 , you get 14 which is 1 * 1 + 2 * 2 + 3 * 3 . 处理最后一个元素3 ,得到14 ,即1 * 1 + 2 * 2 + 3 * 3

I would expect a solution to this problem to look more like: 我希望该问题的解决方案更像是:

import java.util.*;

public class SomaQuadrados {

    public static void main(String[] args) 
    {
        List<Integer> numeros = new ArrayList<>();
        Scanner teclado = new Scanner(System.in);

        while (teclado.hasNextInt()) {
            int numero = teclado.nextInt();
            numeros.add(numero);
        }

        System.out.println(somaQuadrados(numeros));
    }

    public static int somaQuadrados(List<Integer> numeros)
    {
        int size = numeros.size();

        if (size == 0)
        {
            return 0;
        }

        int numero = numeros.get(0);

        return numero * numero + somaQuadrados(numeros.subList(1, size));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 求从 1 到 N 的数字的平方和? (没有之前的总和) - Finding the sum of the squares of the numbers from 1 to N? (Without previous sum) 通过递归方法将1加到N / 2并将N / 2加到N来计算和1至N - Calculating sum 1 to N by adding 1 to N/2 with N/2 to N in a recursive method 我如何编写递归方法来求和 x^n + x^(n-1) + x^(n-2)? - How do i write a recursive method to sum x^n + x^(n-1) + x^(n-2)? 如何在Java中创建执行n的exp但返回String n次的递归方法 - How can i create a recursive method that does the exp of n but returns a String n number of times in Java 如何创建实现递归二进制搜索以在数组中搜索n个数字的循环? JAVA - How do i create a loop that implement a recursive binary search to search for n numbers in a array? JAVA 使用Java递归方法绘制正方形 - drawing squares using a recursive method with java 如何在 Java 中的递归方法中获取 Sum - How to get Sum in a recursive method in Java 生成总和为 1 的 N 个数字 - Generating N numbers that sum to 1 编写一个方法来计算前n个奇数的和 - Write a method to work out the sum of the first n odd numbers 递归方法,将计算至少n个整数的数组中前n个整数的和。 以第n个整数开头 - Recursive method that will compute the sum of the first n integers in an array of at least n integers. Begin with nth integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM