从 C# 中的配对数字制作数字列表
Make list of number from paired number in C#
我有一个子列表列表,其中包含成对的索引号及其二进制值。例如:
Variable Value
route.x[0,0] 0
route.x[0,1] 1
route.x[0,2] 0
route.x[0,3] 0
route.x[1,0] 0
route.x[1,1] 0
route.x[1,2] 0
route.x[1,3] 1
route.x[2,0] 0
route.x[2,1] 0
route.x[2,2] 0
route.x[2,3] 0
route.x[3,0] 0
route.x[3,1] 0
route.x[3,2] 1
route.x[3,3] 0
如果 route.x[i,j]
的值为 1
,则创建一个新列表,其中依次包含该数字。对于该示例,新列表将是:route = 0 1 3 2
到目前为止,我已经制作了这段代码:
//find optimal route
var route = new List<List<int>>();
for (int j = 0; j < C+1; ++j)
{
if (routeopt.x[0, j] != 1)
continue;
List<int> subroute = new List<int>();
subroute.Add(0);
subroute.Add(j);
route.Add(subroute);
}
这段代码的结果是route = 0 1
。之后我使用此代码添加新号码(3
和 2
)。
for (int i = 1; i < C+1; ++i)
{
for (int j = 1; j < C+1; j++)
{
if (routeopt.x[i, j] == 1)
{
List<int> targetlist = route.Single(r => r.Contains(i));
targetlist.Add(j);
}
}
}
如果我有一个 route.x[i,j] 并且有序数中的值为 1,则此代码有效。但是如果没有排序,例如(我只显示值为1的变量):
Variable Value
route.x[0,4] 1
route.x[0,3] 1
route.x[4,1] 1
route.x[1,2] 1
应该是route = 0 3
和route = 0 4 1 2
。但它显示 Sequence contains no matching element
因为索引 1
不包含在 route = 0 3
或 route = 0 4
中。如何处理那个问题?谢谢
Single
方法需要恰好有 1 个 return 值。如果不是,它将抛出异常。
尝试SingleOrDefault
。如果没有找到元素,这将 return null
。
List<int> targetlist = route.SingleOrDefault(r => r.Contains(i));
if(targetList != null)
targetlist.Add(j);
编辑:
如果有 2 个列表包含 i
,这仍然会崩溃。
为避免这种情况,您可以使用 FirstOrDefault
试试下面的代码。它 returns 所有路由列表。 int 列表的路由顺序相反,因此 displaying/using 你应该注意它。
const int C = 4;
static int[,] route_x = new int[5, 5];
static void Main(string[] args)
{
var allRoutes = FindRoutes();
System.Console.ReadLine();
}
private static List<List<int>> FindRoutes()
{
route_x[0, 0] = 0;
route_x[0, 1] = 1;
route_x[0, 2] = 0;
route_x[0, 3] = 0;
route_x[1, 0] = 0;
route_x[1, 1] = 0;
route_x[1, 2] = 0;
route_x[1, 3] = 1;
route_x[2, 0] = 0;
route_x[2, 1] = 0;
route_x[2, 2] = 0;
route_x[2, 3] = 0;
route_x[3, 0] = 0;
route_x[3, 1] = 0;
route_x[3, 2] = 1;
route_x[3, 3] = 0;
route_x[0, 4] = 1;
route_x[0, 3] = 1;
route_x[4, 1] = 1;
route_x[1, 2] = 1;
var routes = new List<List<int>>();
for (int i = 0; i < C + 1; i++)
{
if (route_x[0, i] == 1)
{
var subroutes = FindNextRoute(i);
foreach (var item in subroutes)
{
item.Add(0);
routes.Add(item);
}
}
}
return routes;
}
private static List<List<int>> FindNextRoute(int i)
{
var subroute = new List<List<int>>();
bool found = false;
for (int j = 0; j < C + 1; j++)
{
if (route_x[i, j] == 1)
{
found = true;
var tempRoutes = FindNextRoute(j);
foreach(var item in tempRoutes)
{
item.Add(i);
subroute.Add(item);
}
}
}
if (!found)
{
var singleitem = new List<int>();
singleitem.Add(i);
subroute.Add(singleitem);
}
return subroute;
}
我已经自己找到了。获得第一部分后,我使用此代码添加新号码。这是我的代码:
foreach (var subroute in route)
{
int r = 0;
while (r != subroute[subroute.Count - 1])
{
r = subroute[subroute.Count-1];
for (int j = 1; j < C + 1; j++)
{
if (routeopt.x[r, j] == 1)
subroute.Add(j);
}
}
}
我有一个子列表列表,其中包含成对的索引号及其二进制值。例如:
Variable Value
route.x[0,0] 0
route.x[0,1] 1
route.x[0,2] 0
route.x[0,3] 0
route.x[1,0] 0
route.x[1,1] 0
route.x[1,2] 0
route.x[1,3] 1
route.x[2,0] 0
route.x[2,1] 0
route.x[2,2] 0
route.x[2,3] 0
route.x[3,0] 0
route.x[3,1] 0
route.x[3,2] 1
route.x[3,3] 0
如果 route.x[i,j]
的值为 1
,则创建一个新列表,其中依次包含该数字。对于该示例,新列表将是:route = 0 1 3 2
到目前为止,我已经制作了这段代码:
//find optimal route
var route = new List<List<int>>();
for (int j = 0; j < C+1; ++j)
{
if (routeopt.x[0, j] != 1)
continue;
List<int> subroute = new List<int>();
subroute.Add(0);
subroute.Add(j);
route.Add(subroute);
}
这段代码的结果是route = 0 1
。之后我使用此代码添加新号码(3
和 2
)。
for (int i = 1; i < C+1; ++i)
{
for (int j = 1; j < C+1; j++)
{
if (routeopt.x[i, j] == 1)
{
List<int> targetlist = route.Single(r => r.Contains(i));
targetlist.Add(j);
}
}
}
如果我有一个 route.x[i,j] 并且有序数中的值为 1,则此代码有效。但是如果没有排序,例如(我只显示值为1的变量):
Variable Value
route.x[0,4] 1
route.x[0,3] 1
route.x[4,1] 1
route.x[1,2] 1
应该是route = 0 3
和route = 0 4 1 2
。但它显示 Sequence contains no matching element
因为索引 1
不包含在 route = 0 3
或 route = 0 4
中。如何处理那个问题?谢谢
Single
方法需要恰好有 1 个 return 值。如果不是,它将抛出异常。
尝试SingleOrDefault
。如果没有找到元素,这将 return null
。
List<int> targetlist = route.SingleOrDefault(r => r.Contains(i));
if(targetList != null)
targetlist.Add(j);
编辑:
如果有 2 个列表包含 i
,这仍然会崩溃。
为避免这种情况,您可以使用 FirstOrDefault
试试下面的代码。它 returns 所有路由列表。 int 列表的路由顺序相反,因此 displaying/using 你应该注意它。
const int C = 4;
static int[,] route_x = new int[5, 5];
static void Main(string[] args)
{
var allRoutes = FindRoutes();
System.Console.ReadLine();
}
private static List<List<int>> FindRoutes()
{
route_x[0, 0] = 0;
route_x[0, 1] = 1;
route_x[0, 2] = 0;
route_x[0, 3] = 0;
route_x[1, 0] = 0;
route_x[1, 1] = 0;
route_x[1, 2] = 0;
route_x[1, 3] = 1;
route_x[2, 0] = 0;
route_x[2, 1] = 0;
route_x[2, 2] = 0;
route_x[2, 3] = 0;
route_x[3, 0] = 0;
route_x[3, 1] = 0;
route_x[3, 2] = 1;
route_x[3, 3] = 0;
route_x[0, 4] = 1;
route_x[0, 3] = 1;
route_x[4, 1] = 1;
route_x[1, 2] = 1;
var routes = new List<List<int>>();
for (int i = 0; i < C + 1; i++)
{
if (route_x[0, i] == 1)
{
var subroutes = FindNextRoute(i);
foreach (var item in subroutes)
{
item.Add(0);
routes.Add(item);
}
}
}
return routes;
}
private static List<List<int>> FindNextRoute(int i)
{
var subroute = new List<List<int>>();
bool found = false;
for (int j = 0; j < C + 1; j++)
{
if (route_x[i, j] == 1)
{
found = true;
var tempRoutes = FindNextRoute(j);
foreach(var item in tempRoutes)
{
item.Add(i);
subroute.Add(item);
}
}
}
if (!found)
{
var singleitem = new List<int>();
singleitem.Add(i);
subroute.Add(singleitem);
}
return subroute;
}
我已经自己找到了。获得第一部分后,我使用此代码添加新号码。这是我的代码:
foreach (var subroute in route)
{
int r = 0;
while (r != subroute[subroute.Count - 1])
{
r = subroute[subroute.Count-1];
for (int j = 1; j < C + 1; j++)
{
if (routeopt.x[r, j] == 1)
subroute.Add(j);
}
}
}