带for循环的阵列过程 [英] Array processes with for loop

查看:107
本文介绍了带for循环的阵列过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我有数组,以说明;

Hello,
I have arrays, To illustrate;

reached==false;
int [,] aaa = {{1,6},{9,6},{9,13},{12,16}};
int [,] bbb = {{6,7},{6,11},{6,2},{16,11}};
int [,] ccc = {{11,6},{7,6},{11,16}};



我想做的是从aaa整数开始,设法以某种特殊的方式转到c [x,1]
我将在以下几行中表达的规则:

(x,y,z是来自for循环的int变量)
如果aaa [x,1] == bbb [y,0] ----满足第一个条件.如果满足,请继续搜索下一个数组.即,如果满足bbb [y,1] == ccc [z,0],则将满足所有条件.
要以示例的形式显示在我们的数组中,

{1, 6 }-> { 6 11 }-> { 11 ,16}//可以从aaa [0,1]转换为ccc [2,1]

因此,方向为6-> 11-> 16是可以的.已达到== true;
还有一个条件,每个数字都可以使用一次.

例如:如果是这样的话:{1,6}-> {6,7}-> {7,6} | 1-> 6 -> 7-> 6 无效.达到==假;因为它两次包含数字6".

我想做的就是在找到正确的序列后
有两件事要做:
达到==真;休息; //退出循环

我该如何使用C#来做所有这些事情?



What i want to do is to start from aaa integer to manage to go to c[x,1] in some spesific
rules which i will express in below lines:

(x,y,z are int variables coming from for loops)
if aaa[x,1]==bbb[y,0] ---- first condition is met. If it is met continue with searcing next array. Namely, if bbb[y,1]==ccc[z,0] is met, all conditions will be met.
To show it in our arrays with an example,

{1,6} -> {6,11} -> {11,16} //can go from aaa[0,1] to ccc[2,1]

So the direction is 6->11->16 is okay. reached==true;
One more condition, every number can be used one time.

ex: if it goes in this way : {1,6}-->{6,7}-->{7,6} | 1->6->7->6 is not valid. reached==false; Because it contains "number 6" two times.

What i want to do exactly is that after completing finding any correct series,
two things to be done :
reached== true; break; //exit for loop

How can i do all these things with c#?

Thanks in advance!

推荐答案

像您这样的声音正在通过以下方式进行:
Sounds like you are working your way through something like this: Combinatorics and Graph Theory[^]

Don''t do it like this
int [,] aaa = {{1,6},{9,6},{9,13},{12,16}};
int [,] bbb = {{6,7},{6,11},{6,2},{16,11}};
int [,] ccc = {{11,6},{7,6},{11,16}};



您需要一组节点,每个节点由一个ID和连接的节点之间的边缘标识.首先创建一个Node类,然后创建一个Edge类,然后将节点添加到字典中(以加快查找速度),然后根据您的信息构造图形.

最好的问候
Espen Harlinn



You need a set of nodes, each identified by an id and edges between connected nodes. Start out by creating a Node class, then an Edge class, add the nodes to a dictionary (for faster lookup), construct the graph based on your information.

Best regards
Espen Harlinn


这种表述更好,但您忽略了我的部分建议:您应该像数学中那样以正式的方式表述它.您仍然尝试在示例中进行解释.仍不清楚.使用一些未定义的术语,例如转到",特定规则".

您知道求解算法,因此可以在纸上做吗?如果是这样,只需将其移入代码即可.

但是,如果您在纯编程方面有问题,我怀疑您可能只想念一个细节,即如何确定循环变量范围(不好的说法,但它被广泛使用;更好的术语是域").

这是东西:
http://msdn.microsoft.com/en-us/library/system.array.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.array. getlowerbound.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.array. getupperbound.aspx [ ^ ].

例如:
This formulation is better, but you ignored part of my advice: you should have formulated it in formal way as in mathematics. You still try to explain it all on examples. Still not clear. Use some undefined terms like "go from to", "specific rules".

Do you know the solution algorithm, so you could do in on paper? If so, just move it into code.

However, if you have a problem in pure programming, I suspect you could miss just one detail, how to determine loop variable ranges (bad term, but it''s widely used; better term would be "domains").

Here is the thing:
http://msdn.microsoft.com/en-us/library/system.array.aspx[^],
http://msdn.microsoft.com/en-us/library/system.array.getlowerbound.aspx[^],
http://msdn.microsoft.com/en-us/library/system.array.getupperbound.aspx[^].

For example:
int aaa0Lower = aaa.GetLowerBound(0); //0
int aaa0Upper = aaa.GetUpperBound(0); //1
int aaa1Upper = aaa.GetUpperBound(1); //3
//...
int ccc0Upper = ccc.GetUpperBound(0); //1
int ccc1Upper = ccc.GetUpperBound(1); //2



查看此类的其他属性和方法,这很有用.

你有主意吗?还要别的吗?嵌套循环,中断,可变作用域?基本概念,但是您需要了解所有内容.如果这仍然是算法,那么您仍然无法清楚,正式地解释所有内容.将这些模糊的语句转换为形式系统几乎与编程相同.学习一个,你就会在另一个方面做得很好.

-SA



Look at other properties and method of this class, this is useful to know.

Are you getting the idea. Anything else? Nested loops, breaks, variable scopes? Elementary notions, but you need to know it all. If this is still the algorithm, you still did not explain everything clearly and formally. And translation of those vague statements into the formal system is almost the same as programming. Learn one and you will do well in another.

—SA


static void Main(string[] args)
{
    int[,] aaa = { { 1, 6 }, { 9, 6 }, { 9, 13 }, { 12, 16 } };
    int[,] bbb = { { 6, 7 }, { 6, 11 }, { 6, 2 }, { 16, 11 } };
    int[,] ccc = { { 11, 6 }, { 7, 6 }, { 11, 16 } };


    int i, j, k;
    for (i = 0; i <= aaa.GetUpperBound(0); i++)
        for (j = 0; j <= bbb.GetUpperBound(0); j++)

            if (aaa[i, 1] == bbb[j, 0])
            {
                for (k = 0; k <= ccc.GetUpperBound(0); k++)
                    if (ccc[k, 0] == bbb[j, 1])
                    {
                        int[] direction = { aaa[i, 1], bbb[j, 1], ccc[k, 1] };
                        if (direction[0] != direction[2] &&
                             direction[1] != direction[2] &&
                             direction[0] != direction[1])

                            Console.WriteLine(string.Format("{0},{1} > {2},{3} > {4},{5}", aaa[i, 0], aaa[i, 1], bbb[j, 0], bbb[j, 1], ccc[k, 0], ccc[k, 1]));
                    }
            }
    Console.ReadKey();
}


输出将是:
1,6> 6,11> 11,16
9,6> 6,11> 11,16
12,16> 16,11> 11,6

解决起来需要一个小时.
如果我给您您所需要的,请接受.


The output will be:
1,6 > 6,11 > 11, 16
9,6 > 6,11 > 11,16
12,16 > 16,11 > 11,6

It takes an hour to solve it.
Accept it if I give you what you needed.


这篇关于带for循环的阵列过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆