- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个问题。我尝试实现 Zig-Zag 算法 ( Rail Fence )。
我的代码如下所示:
int n = 3;
int j = 0;
int charCounter = 0;
char[,] chars = new char[n, input.Length]; //array to store chars
while(charCounter <= input.Length) //char counter related to input string
{
if (charCounter >= input.Length)
break;
chars[nCounter++, j++] = input[charCounter++]; //goes n = 0 => 1 => 2 => 3
if (nCounter == n)
for (int i = nCounter; i >= 0; i--) //from this loop, i want to go n => 3 => 2 => 1 => 0 etc
{
if (charCounter >= input.Length)
break;
if (nCounter == 0)
continue;
chars[--nCounter, j++] = input[charCounter++];
} //here i get an exception
}
从上面的示例中我得到一个Exception
:
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
我的问题是,我的代码哪里有错误?
当我从这里更改嵌套 for 循环中的行时:chars[--nCounter, j++] = input[charCounter++];
对此:chars[nCounter--, j++] = input[charCounter++];
我没有得到任何异常,但我的 char 数组看起来像这样:
char[0,0] = input[0];
char[1,1] = input[1];
char[2,2] = input[2];
char[2,3] = input[3]; //that is wrong, should be [1,3]
char[1,4] = input[4];
//and so on..
它应该是这样的:
char[0,0] = input[0];
char[1,1] = input[1];
char[2,2] = input[2];
char[1,3] = input[3]; //here the diffrence
char[0,4] = input[4];
//and so on..
感谢您提出改进我的代码的任何建议!
编辑:根据评论,我做了一些改进:
for(int i = 0; i <= input.Length; i++)
chars[i % n, i] = input[i];
按行迭代非常好,现在我需要解决列问题
最佳答案
有一种方法可以生成一个从 0 递增到 N 递减回到 0 的数列。
考虑以下几点:
对于 N=2 ,序列是 {0,1,0,1 ..}
对于 N=3 ,序列是 {0,1,2,1,0,1,2 ..}
如果我们有一个像这样的序列,我们可以枚举它们,模数为 N+1** :
对于 N=3 ,{0,1,2,1}
对于 N=4 ,{0,1,2,3,2,1}
要生成这个序列,我们必须:从0数到N。然后从 N-1 数到 0+1。并将两者结合在一起。
static int[] GenerateIncreasingDecreasing(int level)
{
var tempRange = Enumerable.Range(0, level).ToArray();
var indexMap = (tempRange.Length < 2) ?
tempRange :
tempRange.Concat(Enumerable.Range(1, level-2).Reverse());
return indexMap.ToArray();
}
那么 Crypt 函数将是:
static string ZigZag(string input, int level)
{
var indexMap = GenerateIncreasingDecreasing(level);
var result =
input.Select((c, i) => new {
Char = c,
Index = indexMap[i % ((level>2)?(level + 1):level)]
})
.GroupBy(x => x.Index)
.OrderBy(g => g.Key)
.SelectMany(x => x.Select(y => y.Char))
.ToArray();
return new string(result);
}
** :对于 N < 2,没有重复。所以模一定是N。
关于c# - 之字形,IndexOutOfRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55003316/
我想在文本框中显示数据库中的某些列。运行代码时,@ApplicantUsername出现错误 IndexOutOfRangeException 这是我的代码: cs.Open();
我基本上需要将任意数量的内容数组存储为一个整数,但之后我必须将其全部回显。 我收到索引超出范围错误。 for (int index = 0; index 0; index--, userArray-
当我尝试访问特定数组索引时,我正在寻找一种方法来防止 IndexOutOfRangeException。 我有一个通用代码,有时在 array[index] 中有值,有时则没有。 所以,在尝试获取它的
我不知道为什么:float val = spectrum[i];正在产生超出数组范围的索引。请帮忙...!我是这个游戏的新手。 public class InputScript : MonoBehav
我正在编写一个 Unity 脚本来读取 CSV 文件(二维和所有数字),将其分解为附加到二维 float 组的 float 。这是我的代码: using System.Collections; usi
这个问题在这里已经有了答案: What does IndexOutofRangeException mean? (3 个答案) 关闭 9 年前。 string Query = "SELECT [AA
我不明白为什么我会在下面的代码中遇到超出范围的异常。 _maxRowIndex 和 _maxColIndex 的值分别为 5 和 0。当 row 和 col 都等于 0 时,第一次抛出异常。我不明白为
程序在运行时说索引超出范围,但我不知道为什么。 错误信息指出的行是 点[计数器 + ((int)(radius * 100))].X = i; 如果那个有错误,下一个(具有相同索引)也一定有错误。 P
这个问题在这里已经有了答案: C# Creating an array of arrays (5 个答案) 关闭 7 年前。 我遇到了这个奇怪的 IndexOutOfRangeException 异
我正在制作一个简单的购物车。它从另一个页面上的按钮将产品添加到购物车页面,我的添加到购物车按钮如下所示 protected void addCart_Click(object sender, Eve
我不明白为什么我在下面的 Where 子句中得到它。 using System; using System.Linq; public static class Extensions { ///
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
我有一个问题。我尝试实现 Zig-Zag 算法 ( Rail Fence )。 我的代码如下所示: int n = 3; int j = 0; int charCounter
在 Form1 构造函数中我有: if (System.IO.File.Exists(keywords_path_file)) { ListBo
我在一些任务中使用字典。 从逻辑上讲,我已将其设置为我的键永远不会发生冲突,但有时在我向字典添加内容时会出现此异常。 Index was outside the bounds of the array
我们目前在我们的一个应用程序中使用 SpreadsheetGear 来生成 Excel 工作簿。我试图在 ASP.NET Web 应用程序中重用其中的一些功能,但到目前为止我很难过。 我首先在 Web
我有以下映射: public class SecurityMap : ClassMap { public SecurityMap() {
我有一个非常简单的类,给出了一个奇怪的错误。该类只有1个属性,查询非常简单。最可怕的是,这似乎是随机发生的。收到此错误后,通常刷新页面可以使其变路,并且应用程序不会再出现此错误。 数据库连接可能有问题
我又遇到了另一个问题。我试图使用 DataReader 从数据库中获取数据,但在测试代码时出现错误。谁能帮我吗?错误发生在这一行: chkAssess = readAssess[columnName]
我还有另外两个按钮,代码几乎完全相同。但是,当我尝试运行此代码以获取所有结果时,出现错误“System.IndexOutOfRangeException”。 显示无效 NHS 号码按钮有效,但我不明白
我是一名优秀的程序员,十分优秀!