需要关于奇怪循环的逻辑帮助

发布于 2024-11-14 17:57:58 字数 1480 浏览 3 评论 0原文

我正在尝试创建一个循环,该循环将递归遍历字节数组并将它们与数组中的下一个字节进行比较(大概使用 for 循环来迭代每个条目)。如果两者相同,我需要它增加一个 int 变量,然后继续。如果两者不同,则需要将 int 变量添加到列表中,后跟其自己的字节值,然后需要“采用”这个新值作为自己的值,并重新开始整个过程​​,直到数组末尾。这是一件非常奇怪的事情,但需要以这种方式完成,以便我可以以正确的顺序和正确的值将 int/byte 对写入文件。

这里关心的是获取相同的连续条目的数量,记下它,然后转到下一个值并重复。例如,如果我们遇到的值 3 与值 1 相同,那么我们就不用关心。只要我们获得值 3 的连续条目数,我们就完成了我们的工作。

其他一些可能有用的要点。

  • 在这种情况下,字节值可以跨越 0-255 的整个范围。
  • 数组大小可能高达 1.5 亿字节条目,因此效率很重要。
  • 数组大小可以预先访问。
  • 最后是字节array 是 Struct 中的字节变量。

我希望这是有道理的。提前致谢。

编辑:抱歉,如果我之前不太清楚,也许我也应该重新命名问题。

澄清一下,我明白我需要在这里做什么,但不知道如何去做。所以我想的问题是如何循环进行此比较,然后在得到错误返回时交换我正在比较的内容。最重要的是,当我比较的内容可能有 255 个值并且我不知道它们时,我该如何做到这一点。我真的无法想象如何编写这个代码,所以我只是坐在那里盯着 VS :)

这更有意义吗?如果没有,我道歉:)

编辑2:如果有人愿意看的话,这是我想出的最终结果。它的灵感来自下面的 aligray 代码。

            int count = 0;
            byte previous = tiles[0].TileTypeId;
            List<int> typeCount = new List<int>();
            List<byte> type = new List<byte>();
            for (int i = 0; i < worldSize; i++)
            {
                byte current = tiles[i].TileTypeId;
                if (previous == current)
                {
                    count++;
                }
                else
                {
                    typeCount.Add(count);
                    type.Add(previous);
                    previous = current;
                    count = 1;
                }
            } 

I'm trying to make a loop that will recurse through an array of bytes and compare them to the next one in the array (presumably using a for loop to iterate through each entry). If the two are the same I need it to increment an int variable and then continue. If the two are not the same it needs to add the int variable to a list followed by its own byte value and then it needs "adopt" this new value as its own and start the whole thing over again until the end of the array. Its a very weird thing but it needs to be done this way so I can write the int/byte pairs to a file in the right order and with the right values.

The concern here is to get the number of consecutive entries that are the same, make note of it, then move on to the next value and repeat. If for example value 3 that we encounter is the same as value 1 that's of no concern to us. As long as we get the number of consecutive entries for value 3 we've done our job.

A couple other possibly helpful points.

  • The byte values could in this case span the full range 0-255.
  • The array size may be up to 150 million byte entries so efficiency is important.
  • The array size is accessible beforehand.
  • Lastly the byte array is a byte variable in a Struct.

I hope this make sense. Thanks in advance.

EDIT: Sorry if I wasn't quite clear before and maybe I should re-title the question as well.

To clarify, I understand what I need to do here just not how to go about it. So the question I guess would be how do I loop through this comparison and then swap out what I'm comparing when I get a false return. Most importantly how do I do that when what I'm comparing could have 255 values and I'm not aware of them. I can't really picture how to code this so I just keep sitting there staring at VS :)

Does that make better sense ? If not I apologize :)

EDIT 2: Here's the end result I came up with if anyone cares to look at it. It was inspired by aligray's code down below.

            int count = 0;
            byte previous = tiles[0].TileTypeId;
            List<int> typeCount = new List<int>();
            List<byte> type = new List<byte>();
            for (int i = 0; i < worldSize; i++)
            {
                byte current = tiles[i].TileTypeId;
                if (previous == current)
                {
                    count++;
                }
                else
                {
                    typeCount.Add(count);
                    type.Add(previous);
                    previous = current;
                    count = 1;
                }
            } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音栖息无 2024-11-21 17:57:59

如果我正确理解了这个问题,希望这能让您开始:

int count = 0;
byte previous = byteArray[0];
List<int> list = new List<int>();

for (int i = 1; i < byteArray.Length; i++)
{
    byte current = byteArray[i];
    if (previous == current)
    {
        count++;
    }
    else
    {
        list.Add(count);
        list.Add(Convert.ToInt32(current));
    }

    previous = current;
} 

If I understood the question correctly, hopefully this will get you started:

int count = 0;
byte previous = byteArray[0];
List<int> list = new List<int>();

for (int i = 1; i < byteArray.Length; i++)
{
    byte current = byteArray[i];
    if (previous == current)
    {
        count++;
    }
    else
    {
        list.Add(count);
        list.Add(Convert.ToInt32(current));
    }

    previous = current;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文