递归算法,总是报堆栈溢出StackOverflowException,运行简单的实例没有问题,当循环达到千万次左右就不行
publicBubbling(int[]tag,intlen,inttype,intres){//初始化全局变量_tag=tag;_len=len;_res=res;_v...
public Bubbling(int[] tag, int len, int type, int res)
{
//初始化全局变量
_tag = tag;
_len = len;
_res = res;
_val = new int[_len];
_type = type;
//初始化数据对象
for (int i = 0; i < _len; i++)
{
_val[i] = _tag[0];
}
}
/// <summary>
/// 外围循环
/// <param name="index">当前索引</param>
/// </summary>
private void Outer(int index)
{
int max = _tag[_tag.Length - 1];
if (_val[index] < max)
{
for (int i = 0; i < _tag.Length; i++)
{
if (_val[index] == _tag[i])
{
_val[index] = _tag[i + 1];
break;
}
}
Inner();
}
else
{
if ((index - 1) > -1)
{
for (int i = index; i < _len; i++)
{
_val[i] = _tag[0];
}
Outer(index - 1);
}
}
}
/// <summary>
/// 内测循环,得到冒泡结果
/// </summary>
public void Inner()
{
for (int i = 0; i < _tag.Length; i++)
{
_val[_len - 1] = _tag[i];
int sum = 0;
string ret = null;
for (int j = 0; j < _val.Length; j++)
{
sum = _type == 0 ? sum + _val[j] : sum * _val[j];
ret += _val[j].ToString().PadLeft(2, '0') + "-";
}
if (sum == _res)
{
result.Add(ret.Substring(0, ret.Length - 1));
}
}
Outer(_len - 2);
}
能说的详细点吗?没有分了!不好意思!
从代码上面说说,谢谢 展开
{
//初始化全局变量
_tag = tag;
_len = len;
_res = res;
_val = new int[_len];
_type = type;
//初始化数据对象
for (int i = 0; i < _len; i++)
{
_val[i] = _tag[0];
}
}
/// <summary>
/// 外围循环
/// <param name="index">当前索引</param>
/// </summary>
private void Outer(int index)
{
int max = _tag[_tag.Length - 1];
if (_val[index] < max)
{
for (int i = 0; i < _tag.Length; i++)
{
if (_val[index] == _tag[i])
{
_val[index] = _tag[i + 1];
break;
}
}
Inner();
}
else
{
if ((index - 1) > -1)
{
for (int i = index; i < _len; i++)
{
_val[i] = _tag[0];
}
Outer(index - 1);
}
}
}
/// <summary>
/// 内测循环,得到冒泡结果
/// </summary>
public void Inner()
{
for (int i = 0; i < _tag.Length; i++)
{
_val[_len - 1] = _tag[i];
int sum = 0;
string ret = null;
for (int j = 0; j < _val.Length; j++)
{
sum = _type == 0 ? sum + _val[j] : sum * _val[j];
ret += _val[j].ToString().PadLeft(2, '0') + "-";
}
if (sum == _res)
{
result.Add(ret.Substring(0, ret.Length - 1));
}
}
Outer(_len - 2);
}
能说的详细点吗?没有分了!不好意思!
从代码上面说说,谢谢 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询