一本通1166 求f(x,n)

【题目描述】

已知

计算x=4.2,n=1以及x=2.5,n=15时f的值。

【输入】

输入x和n。

【输出】

函数值,保留两位小数。

【输入样例】

4.2 10

【输出样例】

3.68


1.看见这种一个套着一个还带着诡异符号的,  基本上都是函数+循环(递归嘛)的套路。  话说这题一开始我没看明白,  从n怎么变到x+1的???  再看看,  好像是由n变到1,然后最后那个根号里面有个x;  让我们递归一下,写写代码。2.这就是正解了
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
double gen(double x,double n);//声明一个double型的函数,最后保留两位输出。
int main()
{
    double n,x;
    double result;
    cin>>x>>n;
    result=gen(x,n);
    printf("%.2lf\n",result);
    return 0;
}
double gen(double x,double n)//定义一下这个“根”函数
{
    if(n==1)
    return sqrt(1+x);
    else
    return sqrt(n+gen(x,n-1));//这时候就是递归最灵魂的操作,调用自己
}

3.该递归的就要递归,

递归的题和一般的函数+循环有区别(其实也不大),

基本上有以下特征:

(1):一个函数套自己,比如f(f(x)),

正常的循环题只有不同参数函数值之间的加法或乘法。

(2):大部分的题,不用递归你做不出来...(或是不好做...)

所以嘛,做题之前一定先想想要用什么算法或者结构什么的,

不然代码打到一半不会打了太尴尬...



原文地址:https://www.cnblogs.com/Jiangxingchen/p/12244693.html

时间: 2024-07-29 01:33:53

一本通1166 求f(x,n)的相关文章

求f(x,n)

求f(x,n) 链接: http://ybt.ssoier.cn:8088/problem_show.php?pid=1166 [题目描述] 已知 计算x=4.2,n=10以及x=2.5,n=15时的f的值. [输入] 输入x和n. [输出] 函数值,保留两位小数. [输入样例] 4.2 10 [输出样例] 3.68 #include <iostream> #include<stdio.h> #include<math.h> using namespace std; d

求f(k)=k^k(k=1...n)的前n项和

求f(k)=k^k(k=1...n)的前n项和. 程序实现: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> long long My_Mul_Sum(int *n)//封装了一个求k^k的前n项和的函数 { int k = 1; long long sum = 0;//定义为long long是为了防止数据较大,容易溢出 for (k = 1; k <= n; k++) { int count = 0, mul = 1;//count

hdu 1588 求f(b) +f(k+b) +f(2k+b) +f((n-1)k +b) 之和 (矩阵快速幂)

g(i)=k*i+b; 0<=i<nf(0)=0f(1)=1f(n)=f(n-1)+f(n-2) (n>=2)求f(b) +f(k+b) +f(2*k+b) +f((n-1)*k +b) 之和 Sample Input2 1 4 100 // k b n MOD2 0 4 100 Sample Output2112 矩阵A      相当于 1 1          f(2)  f(1) 1 0          f(1)  f(0) | 1       1| ^b          |

YT14-HDU-三分查找求F(x)的最小值

Problem Description Now, here is a fuction: F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100) Can you find the minimum value when x is between 0 and 100. Input The first line of the input contains an integer T(1<=T<=100) which means the numb

再求f(x,n)

再求f(x,n) 链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1167 [题目描述] 已知 用递归函数求解. [输入] 第一数是x的值,第二个数是n的值. [输出] 函数值. [输入样例] 1 2 [输出样例] 0.40 #include <iostream> #include<stdio.h> #include<math.h> using namespace std; double h(int x,int n){

使用递归和非递归求f(m,n)

递归方法如下: int f(int m, int n) { if (1 == m) { return n; } else if (1 == n) { return m; } return f(m, n - 1) + f(m - 1, n); } 非递归方法如下: int f(int m, int n) { int a[100][100]; for (int i = 0; i < m; ++i) { a[i][0] = i + 1; } for (int i = 0; i < n; ++i) {

求渐近线

求$f(x)=\frac{x^{1+x}}{(1+x)^{x}}(x>0)$的斜渐近线 (i).斜渐近线系数 $$a=\lim_{x\to\infty}\frac{f(x)}{x}=\lim_{x\to\infty}\left(1-\frac{1}{x+1}\right)^{x}=e^{-1}$$ 方法一: (ii)$$b=\lim{x\to\infty}f(x)-ax=\lim_{x\to\infty}\frac{x}{e}\left(e\frac{x^{x}}{(1+x)^{x}}-1\ri

hunnu-11546--Sum of f(x)

Sum of f(x) Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Total submit users: 194, Accepted users: 115 Problem 11546 : No special judgement Problem description   令f(x)为x的全部约数之和,x的约数即能够被x整除的数,如f(24)=1+2+3+4+6+8+12+24=60),求 f(l) +

算法题:求一个整数的开方

#include <iostream> #include <math.h> using namespace std; double Grial(int x) { double result = 1; double num = x; do{ result = num; num = result/2.0+x/2.0/result; }while(fabs(num-result)>0.00001); return result; } //牛顿迭代公式x(n+1)=x(n)-f(x(