Description
计算下列表达式值:
Input
输入x和n的值,其中x为非负实数,n为正整数。
Output
输出f(x,n),保留2位有效数字。
Sample Input
3 2
Sample Output
2.00
【AC代码】:
#include <iostream>
#include <stdio.h>
using namespace std;
#include <stdio.h>
#include<math.h>
double f(int x, int n)
{
if(n==1)
return sqrt(1+x);
return sqrt(n+f(x,n-1));
}
int main()
{
int x,n;
cin>>x>>n;
printf("%.2f",(f(x,n)));
return 0;
}
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x=sc.nextInt();
int n=sc.nextInt();
DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(f(x,n)));
}
private static double f(int x, int n) {
// TODO Auto-generated method stub
if(n==1)
return Math.sqrt(1+x);
return Math.sqrt(n+f(x,n-1));
}
}