超级难题,高手进来挑战挑战看!50分哟!!!!!!!!!!
实现如下功能:
当传入参数string a = "1 ";时
for (int i = 0; i < a.Length; i++)
sum += i;
当传入参数string a = "12 ";时
for (int i = 0; j < a.Length; i++)
for (int j = 0; j < i; j++)
sum += (i + j);
当传入参数string a = "123 ";
for (int i = 0; j < a.Length; i++)
for (int j = 0; j < i; j++)
for (int k = 0; k < j; k++)
sum += (i + j + k);
假设string a = "123456... "
依次类推...
设计一个方法:
static int fun(string a)
{
int sum = 0;
//在此写代码
return sum;
}
求解注释位置的代码:)
------解决方案--------------------0> 1> 2> 3> 4....> a.Length-1,满足条件的只有一种情况,就是0+1+2+3+...+a.Length-1
------解决方案--------------------Sub DoIt(ByRef Sum As Integer, ByVal StrCode As String, ByVal CurLength As Integer, ByVal TempSum As Integer, ByVal Cyc As Integer)
Dim II As Integer
For II = 0 To CurLength - 1
If Cyc = StrCode.Length Then
Sum = TempSum + II
Else
Cyc += 1
TempSum += II
DoIt(Sum, StrCode, II, TempSum, Cyc)
End If
Next
End Sub
Function GetSum(ByVal StrCode As String) As Integer
Dim Sum As Integer
DoIt(Sum, StrCode, StrCode.Length, 0, 1)
Return Sum
End Function
Private Sub MyButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyButton1.Click
MsgBox(GetSum( "abcd "))
End Sub
不知道是否正确哈~~简单的写了一下,思路没问题,但是可能算法不够简练;
第归应该还是可以实现的,回头转换成C#给你,我不大会C#,可能会有问题