Java实现 蓝桥杯VIP 算法训练 非递归(暴力)

试题 算法训练 非递归

问题描述
  当x>1时,Hermite多项式的定义见第二版教材125页。用户输入x和n,试编写“非递归”函数,输出对应的Hermite多项式的值。其中x为float型,n为int型。
输入格式
  x n
输出格式
  对应多项式的值
样例输入
一个满足题目要求的输入范例。
例:
3.6 4
样例输出
与上面的样例输入对应的输出。
例:
2077.31
数据规模和约定
  x>1
  n为自然数

PS:
百度搜索那个Hermite多项式的原理

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        float x = sc.nextFloat();
        float n = sc.nextFloat();
        if (n < 5)
            System.out.printf("%.2f", fac(x, n));
        else
            System.out.printf("%.3f", fac(x, n));
    }

    public static float fac(float x, float n) {
        if (n == 0) return 1;
        if (n == 1) return 2 * x;
        return (float) (2.0 * x * fac(x, n - 1) - 2.0 * (n - 1) * fac(x, n - 2));
    }
}

原文地址:https://www.cnblogs.com/a1439775520/p/13074643.html