1165:Hermite多项式(改正)

题目要求:用递归的方法求Hermite多项式的值
具体的分段函数,到题目上看
输入:1 2
输出:4.00

#include<iostream>
#include<iomanip>
using namespace std;

void Cin(); 
int Solution(int m, int n);
int n, k;
int coun = 1;

int main(void){
	Cin();
	double s = double(Solution(n, k));//强制转换 (double)a, double(a) 
	cout << setiosflags(ios::fixed) << setprecision(2);//这是C++中格式化输出的方式 
	cout << s << endl;
	
	return 0;
}

void Cin(){
	cin >> n >> k;
}

int Solution(int n, int k){
	if (n == 0)
		return 1;
	else if (n == 1)
		return 2 * k;
	else if (n > 1)
		return 2 * k * Solution(n - 1, k) - 2 * (n - 1) * Solution(n - 1, k);
}

cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2):输出一个右对齐的小数点后两位的浮点数。

setprecision(n):控制输出流显示浮点数的数字个数。

setiosflags(ios::fixed):用定点方式表示实数。

iso::right :在指定区域内右对齐输出。

cout:输出。

修改:因为之前没有看到x是实数,而且因为变量不一致,导致函数写错了。
最重要的一点,全局变量不能随便用。。麻烦

#include<iostream>
#include<iomanip>
using namespace std;

double Solution(double x, int n);
int coun = 1;

int main(void){
	double x;
	int n;
	
	cin >> n >> x;
	double s = Solution(x, n);//强制转换 (double)a, double(a) 
	cout << setiosflags(ios::fixed) << setprecision(2);//这是C++中格式化输出的方式 
	cout << s << endl;
	
	return 0;
}

double Solution(double x, int n){
	if (n == 0)
		return 1;
	else if (n == 1)
		return 2 * x;
	else if (n > 1)
		return (2 * x * Solution(x, n - 1) - 2 * (n - 1) * Solution(x, n - 2));
}
发布了17 篇原创文章 · 获赞 0 · 访问量 269

猜你喜欢

转载自blog.csdn.net/qq_45818773/article/details/102872946