计算元素对 [英] Counting pairs of elements

查看:65
本文介绍了计算元素对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个像这样的数组1,1,2,2,1,1,2,2,1,3,3,3,3,3,4,4,我需要计算多少将一对元素相隔距离d乘以该数组中的元素对的倍数.例如d = 2,x [0] = 1; x [0 + d] = 2;对于1-2对,我们在数组中出现4次;
这个问题困扰着我,这一定很容易,但是我找不到解决方法.而且阵列会很大,因此也必须要快.

Hello, i have an array something like this 1,1,2,2,1,1,2,2,1,3,3,3,3,3,4,4 and i need to count how many times a pair of elemnts at a distnace d from eachother its in that array. for example d=2, x[0]=1; x[0+d]=2; For pair 1-2 we have 4 occurence in array;
this problem is bugging my mind it must be something easy but i cant find the answer how to do it. And the array is gonna be verry big so it has to be fast also. Can you help me pls?

推荐答案

好吧,我认为这应该不难吗?
首先,弄清您的第一个数字是什么(在示例1中),然后在指定的索引处检查该数字.
现在创建一个for循环,检查任何数字,如果它与您的第一个数字相同,并且还检查当前索引+指定的索引.
像这样的东西:
Well, this shouldn''t be to difficult I think?
You start out by figuring out what your first number is, in your example 1, then you check the number at the specified index.
Now make a for loop that checks any number and if it is the same as your first number and if it is also check the current index + the specified index.
Something like this:
static int GetNumberOfPairs(IEnumerable<int> arr, int distance)
{
    int counter = 0;
    // Check if your collection is big enough to begin with.
    if (arr.Count() > distance)
    {
        // Get the numbers you need.
        int firstNo = arr.ElementAt(0);
        int secondNo = arr.ElementAt(distance);
        // Loop through the collection and stop when adding
        // the index would cause an OutOfRangeException.
        for (int i = 0; i < arr.Count() - distance; i++)
        {
            // If the current index matches the first number,
            // then also check if current index + distance is the second number.
            if (arr.ElementAt(i) == firstNo && arr.ElementAt(i + distance) == secondNo)
            {
                // If it is increment a counter.
                counter++;
            }
        }
    }
    // Return the counter.
    return counter;
}
</int>


我不是C#程序员,所以如果代码看起来很奇怪,请原谅我...
您可能需要稍微调整一下此代码,因为我还没有对其进行太多测试.但这与您的示例相符.
另外,我并不是说这是解决方案.这是一个解决方案.可能还有其他人,也许还有更好的解决方案.当然,这无疑是(可能)最简单的方法:)
希望这对您有帮助!


I''m not a C# programmer, so forgive me if the code looks odd...
You might need to tweak this code a bit since I haven''t tested it very much. But it matched with your example.
Also, I''m not saying this is THE solution. It is A solution. There''s probably others and maybe better solutions. This one is certainly (probably) easiest though :)
Hope this helps!


出于好奇,我冒险尝试其他方法来解决此问题-如Naerling
所述
Out of curiosity, I ventured into other ways of solving this problem - as mentioned by Naerling

//The pattern conveys - the first digit followed by d other digits then followed by the second digit
//firstNo="1"; secondNo="2"; distance="2"; All firstNo,secondNo,distance are String.
Regex regexObj=new Regex(firstNo + @"\d{" + distance + "}" + secondNo);
//Match collection gets all occurences of this regular expression
MatchCollection MatchObj = regexObj.Matches("1122112213333344");
//Just printing the occurence count!
Console.WriteLine(MatchObj.Count);



感觉优雅,而且正如我尝试过的那样,它可以正常工作.

您可以尝试这种方式,让我知道它适合您的问题吗?

PS:这需要使用System.Text.RegularExpressions库.



Feels elegant, and as I''ve tried out, It''s working as it should.

Can you try this way and let me know it''s aptness for your problem?

PS: This requires System.Text.RegularExpressions library to be used.


如何:

How about:

int[] arr = {2, 1, 1, 2, 2, 1, 3, 3, 3, 3, 3, 4, 4};
int d = 2;
int a = 1
int b = 2;
var e = arr.Skip(d).GetEnumerator();
int m = arr.Take(arr.Length-d).Count(v=>e.MoveNext() && v==a && e.Current==b);



不是说我真的会像这样编程... ;-)

干杯

安迪



Not that I would really program like this... ;-)

Cheers

Andi


这篇关于计算元素对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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