读书人

求f(x,n)=x/1!+2!+3!+.+n!用函数实现的

发布时间: 2012-03-23 12:06:21 作者: rapoo

求f(x,n)=x/1!+2!+3!+...+n!用函数实现的代码。
编写一个求f(x,n)的程序,其中f(x,n)=x/1!+2!+3!+...+n! 。程序运行时,在相应的文本框中输入x和n的值,然后单击“计算”按钮,即能计算出f(x,n)的值(要求用函数实现)。

函数搞来搞去搞不出啊!!

[解决办法]
这个n有上限?
[解决办法]
f(x,n)=x/1!+2!+3!+...+n!
相当于
f(x,n)=x+1!+2!+....+n!-1 >>>f(x,n)=1!+...n!+x-1

多写几个函数就行了

VB.NET code
    <TestMethod()> Public Sub TestMethod1()        ' TODO: 在此添加测试逻辑        Console.WriteLine(f(5, 4))    End Sub    Private Function f(ByVal x As Integer, ByVal n As Integer) As Integer        Return x - 1 + f2(n)    End Function    Private Function f1(ByVal n As Integer) As Integer        If n = 1 Then            Return 1        ElseIf n > 1 Then            Return n * f1(n - 1)        End If    End Function    Private Function f2(ByVal n As Integer) As Integer        Dim sum As Integer        For i As Integer = 1 To n            sum += f1(i)        Next        Return sum    End Function 

读书人网 >VB Dotnet

热点推荐