嘿我正在尝试进行代码质询,我已经编写了以下代码。
一个测试用例获得成功,但所有其他测试用例都会出现运行时错误NZEC错误。我坚持了两天。请帮帮我。
问题陈述:
鲍勃非常喜欢排序。他总是在考虑对数组进行排序的新方法。他的朋友拉姆给了他一项挑战性的任务。他给Bob一个数组和一个整数K.挑战是在最多K-swaps之后产生字典最小数组。只能交换连续的元素对。在最多K-swaps之后,帮助Bob返回字典最小数组。输入:
第一行包含整数T(即测试用例的数量)。 T测试案例如下。每个测试用例有2行。第一行包含N(数组中的元素数)和K(交换数)。第二行包含数组的n个整数。
输出:
打印字典最小数组。
样本输入(明文链接)
2 3 2 5 3 1 5 3 8 9 11 2 1
样本输出(明文链接)
1 5 3 2 8 9 11 1
public static long Mostswaps;
public static void Main(string[] args)
{
var line1 = Console.ReadLine().Trim();
var N = long.Parse(line1);
var nk = new string[N];
var numbers = new string[N];
for (long i = 0; i < N; i++)
{
nk[i] = (Console.ReadLine().Trim());
numbers[i] = (Console.ReadLine().Trim());
}
for (long i = 0; i < N; i++)
{
long n;
long.TryParse(nk[i].Split(' ')[0], out n);
long.TryParse(nk[i].Split(' ')[1], out Mostswaps);
var numbersforcurrrent = numbers[i].Split(' ').Select(x => Convert.ToInt64(x)).ToArray();
var numbertotryswap = numbersforcurrrent.Where(x => x < numbersforcurrrent.First()).ToList();
numbertotryswap.Sort();
var minlex = new List<long[]>();
var j = 0;
foreach (var swapnumber in numbertotryswap)
{
minlex.Add(LexicalMin(numbersforcurrrent, Mostswaps, swapnumber));
j++;
}
var minimum = minlex.First().ConvertToLex();
minimum = minlex.Select(ne => ne.ConvertToLex()).Concat(new[] { minimum }).Min();
var minimumlex = minlex.First(x => x.ConvertToLex() == minimum);
foreach (var lex in minimumlex)
{
if (Array.IndexOf(minimumlex, lex) == minimumlex.Length)
{
Console.Write(lex);
}
else Console.Write(lex+" ");
}
Console.WriteLine();
}
}
public static long[] LexicalMin(long[] word, long swapsleft, long numbertotryswap)
{
if (swapsleft == 0)
{
return word;
}
var originalLex = word.ConvertToLex();
var checkword = word.ToArray();
var tempword = word.ToArray();
long currentindex = Array.IndexOf(checkword, numbertotryswap);
if (currentindex != 0)
{
var temp = checkword[currentindex - 1];
tempword[currentindex - 1] = checkword[currentindex];
tempword[currentindex] = temp;
}
swapsleft--;
if (tempword.ConvertToLex() < originalLex && swapsleft == 0)
{
return tempword;
}
tempword = LexicalMin(tempword, swapsleft, numbertotryswap);
return tempword;
}
public static class ListHelper
{
public static long ConvertToLex(this long[] source)
{
var result = source.Aggregate(string.Empty, (current, number) => current + number.ToString());
return Convert.ToInt64(result);
}
}