3个回答
展开全部
Sub Main()
Dim n As Integer
Dim sum, nsum As Integer 'sum 记录总和,nsum记录1+2+..N的和
Console.WriteLine("please input n")
Dim tmpStr As String
tmpStr = Console.ReadLine()
'判断是否是int类型的,
'IsNumeric(tmpStr)判断是否是数字类型,InStr(tmpStr, ".") 判断是否有小数点
If IsNumeric(tmpStr) And InStr(tmpStr, ".") < 1 Then
n = CType(tmpStr, Integer)
Else
MsgBox("not a valid integer!")
Exit Sub
End If
nsum = 0
sum = 0
For i As Integer = 1 To n
nsum = nsum + i
sum = sum + nsum
Next
Console.WriteLine("the sum is : " & sum.ToString & " press enter to exit...")
Console.ReadLine()
End Sub
Dim n As Integer
Dim sum, nsum As Integer 'sum 记录总和,nsum记录1+2+..N的和
Console.WriteLine("please input n")
Dim tmpStr As String
tmpStr = Console.ReadLine()
'判断是否是int类型的,
'IsNumeric(tmpStr)判断是否是数字类型,InStr(tmpStr, ".") 判断是否有小数点
If IsNumeric(tmpStr) And InStr(tmpStr, ".") < 1 Then
n = CType(tmpStr, Integer)
Else
MsgBox("not a valid integer!")
Exit Sub
End If
nsum = 0
sum = 0
For i As Integer = 1 To n
nsum = nsum + i
sum = sum + nsum
Next
Console.WriteLine("the sum is : " & sum.ToString & " press enter to exit...")
Console.ReadLine()
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int Sum(int n)
{
return (n == 1 ? 1 : n + Sum(n - 1));
}
int ComboSum(int n)
{
return (n == 1 ? 1 : Sum(n) + ComboSum(n - 1));
}
{
return (n == 1 ? 1 : n + Sum(n - 1));
}
int ComboSum(int n)
{
return (n == 1 ? 1 : Sum(n) + ComboSum(n - 1));
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
呵呵,自己推下公式吧:
s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)
=n(n+1)(2*n+1)/12 + n(n+1)/4
=n(n+1)(n+2)/6
Public Function SSum(byval n as int32) as int32
Return n*(n+1)*(n+2)/6
End Function
s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)
=n(n+1)(2*n+1)/12 + n(n+1)/4
=n(n+1)(n+2)/6
Public Function SSum(byval n as int32) as int32
Return n*(n+1)*(n+2)/6
End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询