YTU OJ 2020: C语言实验——计算表达式

198 篇文章 53 订阅
52 篇文章 1 订阅

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));
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值