如何正确处理一个阵列 [英] How to process an array correctly

查看:81
本文介绍了如何正确处理一个阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的问题的第一部分,如果你想查这个问题的背景:

Here's the part 1 of my question, if you wanna check the background of this question :

<一个href=\"http://stackoverflow.com/questions/23581002/detecting-brackets-in-input-string/23581913?noredirect=1#23581913\">Detecting在输入字符串括号

原谅我,如果标题不匹配,因为我也困惑如何命名它适当地想象我的问题。如果有人知道一个更合适的标题,随意编辑。

Forgive me if the title doesn't match, since I also confused how to name it appropriately to picture my problem. If anyone knows a more appropriate title, feel free to edit.

所以,低于code(我自己的code)给出:

So, given below code (my own code) :

    private const int PARTICLE_EACH_CHAR = 4;

    /*ProcessBarLines : string s only contains numbers, b, [,  and ]*/
    private int ProcessBarLines(Canvas canvas, string s, int lastLineAboveNotation)
    {
        List<int> bracket = new List<int>();
        List<int> other = new List<int>();
        int currentCloseNumber = 0;
        int currentOpenNumber = 0;

        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] == '[')
            {
                bracket.Add(i);
                currentOpenNumber++;
                if (i - 1 > 0 && s[i - 1] != '[')
                {
                    currentOpenNumber = 1;
                }
            }
            else if (s[i] == ']')
            {
                bracket.Add(i);
                currentCloseNumber++;

                if (i + 1 >= s.Length || s[i + 1] != ']' || currentOpenNumber == currentCloseNumber)
                {
                    int min = bracket.Count - (currentCloseNumber * 2);
                    int max = bracket[bracket.Count - 1];

                    List<int> proc = new List<int>();

                    int firstIndex = -1;
                    int lastIndex = -1;

                    for (int ii = 0; ii < other.Count; ii++)
                    {
                        if (other[ii] > min && other[ii] < max)
                        {
                            proc.Add(other[ii]);

                            if (firstIndex == -1)
                            {
                                firstIndex = ii;
                                lastIndex = ii;
                            }
                            else
                            {
                                lastIndex = ii;
                            }
                        }
                    }

                    double leftPixel = firstIndex * widthEachChar;
                    double rightPixel = (lastIndex * widthEachChar) + widthEachChar;

                    DrawLine(canvas, currentCloseNumber, leftPixel,
                        rightPixel, lastLineAboveNotation * heightEachChar / PARTICLE_EACH_CHAR);

                    lastLineAboveNotation += currentCloseNumber - 1;

                    currentOpenNumber -= currentCloseNumber;
                    currentCloseNumber = 0;
                }
            }
            else
            {
                other.Add(i);
            }
        }

        return lastLineAboveNotation + 1;
    }

下面是测试用例:

照片1安培; 2是正确的答案和图片3是错误的答案。图片3应该有一条线,就像从2号倒,但是,很显然,(如果你仔细观察)绘制直线上的权利,但它应该是在左边是正确的(上述 0 )。

Picture 1 & 2 is the correct answer, and picture 3 is the wrong answer. Picture 3 should have a line, just like inverted from number 2, but, apparently, (if you look closely) the line is drawn on the right, but it should be on the left to be correct (above 0).

我算了一下,问题是,我在MIN相当肯定。因为它没有给出正确的初始值。

I figured, the problem is, I'm quite sure on the "min". Since it doesn't give the correct starting value.

对此有何想法?随意澄清什么。它用于书写数字乐谱。

Any idea on this? Feel free to clarify anything. It's used for writing numeric musical scores.

顺便说一句,的DrawLine()只是为了得出上述数字的线,这是没问题的。

Btw, DrawLine() just meant to draw the line above the numbers, it's not the problem.

推荐答案

终于来了!我找到了!

Finally! I found it!

    private int ProcessBarLines(Canvas canvas, string s, int lastLineAboveNotation)
    {
        List<int> bracket = new List<int>();
        List<int> other = new List<int>();
        int currentCloseNumber = 0;
        int currentOpenNumber = 0;
        int space = 0;

        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] == '[')
            {
                bracket.Add(i);
                currentOpenNumber++;
                if (i - 1 > 0 && s[i - 1] != '[')
                {
                    currentOpenNumber = 1;
                }
            }
            else if (s[i] == ']')
            {
                bracket.Add(i);
                currentCloseNumber++;

                if (i + 1 >= s.Length || s[i + 1] != ']' || currentOpenNumber == currentCloseNumber)
                {

                    int min = bracket[Math.Max(bracket.Count - ((currentCloseNumber * 2) + space), 0)];
                    int max = bracket[bracket.Count - 1];

                    space = max - min - 1;

                    List<int> proc = new List<int>();

                    int firstIndex = -1;
                    int lastIndex = -1;

                    for (int ii = 0; ii < other.Count; ii++)
                    {
                        if (other[ii] > min && other[ii] < max)
                        {
                            proc.Add(other[ii]);
                            other[ii] = -1;
                            if (firstIndex == -1)
                            {
                                firstIndex = ii;
                                lastIndex = ii;
                            }
                            else
                            {
                                lastIndex = ii;
                            }
                        }
                    }

                    double leftPixel = firstIndex * widthEachChar;
                    double rightPixel = (lastIndex * widthEachChar) + widthEachChar;

                    DrawLine(canvas, currentCloseNumber, leftPixel,
                        rightPixel, lastLineAboveNotation * heightEachChar / PARTICLE_EACH_CHAR);

                    lastLineAboveNotation += 1;

                    currentOpenNumber -= currentCloseNumber;
                    currentCloseNumber = 0;
                }
            }
            else
            {
                other.Add(i);
            }
        }

        return lastLineAboveNotation + 1;
    }

如果有人得到了更有效的code,请告诉我们!

If someone got a more efficient code, please let us know!

这篇关于如何正确处理一个阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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