簡體   English   中英

從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 之后,我使用此代碼添加新數字( 32 )。

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 3route = 0 4 1 2 但是它表明Sequence contains no matching element因為route = 0 3route = 0 4不包含索引1 如何處理這個問題? 謝謝

嘗試下面的代碼。 它返回所有路線的列表。 int列表的路徑是相反的,因此在顯示/使用時應注意這一點。

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);
        }
    }
}

Single方法期望返回值正好為1。 如果沒有,它將引發異常。

嘗試SingleOrDefault 如果未找到任何元素,則將返回null

List<int> targetlist = route.SingleOrDefault(r => r.Contains(i));
if(targetList != null)
    targetlist.Add(j);

編輯:

如果有兩個包含i列表,則仍然會崩潰。 為了避免這種情況,您可以使用FirstOrDefault

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM