C# 中的游程长度解码器
这里有人可能有 C# 中的游程解码器吗?我确实需要上述代码。谢谢。
using System;
class RLDEC
{
static void Main()
{
int t = int.Parse(Console.ReadLine());
for (int k = 0; k < t; k++)
{
string s = Console.ReadLine();
s = runLengthDecoder(s);
Console.WriteLine(s);
}
}
static string runLengthDecoder(string s)
{
string d = ""; // decoded string
int cv; // current value
for(int k = 0; k < s.Length; k++)
{
cv = Convert.ToInt32(s[k]) - 48;
if (k + 1 < s.Length && cv != 1 && cv >= 2 && cv <= 9)
{
for(int v = 0; v < cv; v++)
d += s[k+1];
}
if (cv == 1)
{
int z = k + 1;
while(k < s.Length && z < s.Length && Convert.ToInt32(s[z]) - 48 != 1)
{
d += s[z];
z++;
k++;
}
k++;
}
}
return d;
}
}
Is it possible that anyone around here might have a run-length DECODER in C#? I'm in real need of said code. Thanks.
using System;
class RLDEC
{
static void Main()
{
int t = int.Parse(Console.ReadLine());
for (int k = 0; k < t; k++)
{
string s = Console.ReadLine();
s = runLengthDecoder(s);
Console.WriteLine(s);
}
}
static string runLengthDecoder(string s)
{
string d = ""; // decoded string
int cv; // current value
for(int k = 0; k < s.Length; k++)
{
cv = Convert.ToInt32(s[k]) - 48;
if (k + 1 < s.Length && cv != 1 && cv >= 2 && cv <= 9)
{
for(int v = 0; v < cv; v++)
d += s[k+1];
}
if (cv == 1)
{
int z = k + 1;
while(k < s.Length && z < s.Length && Convert.ToInt32(s[z]) - 48 != 1)
{
d += s[z];
z++;
k++;
}
k++;
}
}
return d;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有编码规范,很难解决这个问题,但在这段代码中,
我希望 k 在离开 if 块之前递增。
另外,我希望下一个
if (cv==1)
是一个else if
。在该
cv==1
块中,我认为您应该只处理下一个字符并让外部 for 循环完成其工作。我也不明白这个块是如何工作的
It's hard to solve this without the specification for your encoding, but in this code
I would expect k to be incremented before leaving the if block.
Also, I would expect that the next
if (cv==1)
is anelse if
instead.In that
cv==1
block, I think you should just process then next character and let the outer for loop do its work.I also don't understand how that block could work at all