Problem Description
计算下列表达式值:
Input
输入x和n的值,其中x为非负实数,n为正整数。
Output
输出f(x,n),保留2位小数。
Example Input
3 2
Example Output
2.00
利用C语言写的代码如下:
//利用了函数实现的
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double f(double x,int n);
int main()
{
int n,x;
scanf("%d %d",&x,&n);
printf("式子=%.2f\n",f(x,n));
return 0;
}
double f(double x,int n) //输入n是次数,x是起始值
{
double sum = 0;
int i;
for(i=1;i<=n;i++)
{
sum=sqrt(x+i);
x=sum;
}
return sum;
}
利用C++写的代码如下:
//#include <iostream>
//#include<math.h>
//#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
double f(double x,int n)
{
double sum=0;
for(int i=1;i<=n;i++)
{
sum=sqrt(x+i);
x=sum;
}
return sum;
}
int main()
{
double x;
int n;
cin>>x>>n;
printf("%.2lf\n",f(x,n));
return 0;
}
参照大佬的:https://blog.csdn.net/Lycodeboy/article/details/53046144