在使用函数式语言之后,我开始在Java中使用更多的递归 - 但是语言似乎有一个相对较浅的调用堆栈,大约1000.
有没有办法让调用堆栈更大?就像在Erlang中一样,我可以创建数百万次调用的函数吗?
当我做项目欧拉问题时,我越来越注意到这一点.
谢谢.
我使用CSharpCodeProvider编译我的代码,并在结果汇编中动态创建某个类的实例.比我叫一些方法.如果方法有递归,我得到StackOverflowException,我的应用程序终止.
我该如何避免这种情况?
using System;
using System.Runtime.Remoting;
namespace TestStackOverflow
{
class Program
{
class StackOver : MarshalByRefObject
{
public void Run()
{
Run();
}
}
static void Main(string[] args)
{
AppDomain domain = AppDomain.CreateDomain("new");
ObjectHandle handle = domain.CreateInstance(typeof (StackOver).Assembly.FullName, typeof (StackOver).FullName);
if (handle != null)
{
StackOver stack = (StackOver) handle.Unwrap();
stack.Run();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
给定n个金条,找到适合容量W的金的最大重量
第一行包含背包的容量W和金条的数量n.下一行包含n个整数
适合容量为W的背包的最大黄金重量.
1 <= W <= 10000; 1 <= n <= 300; 0 <= w0,w1,w2,...,w(n-1)<= 100000
#include <iostream>
#include <vector>
using std::vector;
int optimal_weight(int W, vector<int> w) {
int n = w.size() + 1;
int wt = W + 1;
int array [n][wt];
int val = 0;
for(int i = 0; i < wt; i++) array [0][i] = 0;
for(int i = 0; i < n; i++) array …Run Code Online (Sandbox Code Playgroud) c++ arrays algorithm dynamic-programming multidimensional-array
我正在写ac#应用程序.我是c#的新手.
我得到了一个StackOverflowException(是!:D)尝试在构造函数中设置类属性,如下所示:
namespace WindowsUpdateOnLan
{
public class NetworkAdapter
{
public NetworkAdapter(PropertyDataCollection properties)
{
String value = null;
foreach (PropertyData pd in properties)
{
if (pd.Name.Equals("GUID"))
Id = Guid.Parse(pd.Value.ToString());
if (pd.Name.Equals("Name"))
Name = pd.Value.ToString();
if (pd.Name.Equals("NetConnectionID"))
{
value = Regex.Replace(pd.Value.ToString(), @"\s+", "");
adapterType = (AdapterTypeEnum)Enum.Parse(typeof(AdapterTypeEnum), value);
}
if (pd.Name.Equals("NetEnabled"))
{
value = Regex.Replace(pd.Value.ToString(), @"\s+", "");
adapterStatus = (AdapterStatusEnum)Enum.Parse(typeof(AdapterStatusEnum), value);
}
}
}
/// <summary>
/// Contains the GUID that is used to identify the adapter
/// </summary>
public Guid Id
{ …Run Code Online (Sandbox Code Playgroud)