C# 如何计算数组中相同值之间的最大距离
我想写一个程序来计算每个数字和这个数组的相同数字之间的最长距离 这是我的密码C# 如何计算数组中相同值之间的最大距离,c#,console-application,C#,Console Application,我想写一个程序来计算每个数字和这个数组的相同数字之间的最长距离 这是我的密码 int max_dist = 0; for (int j= 0; j < n; j++) { for (int secondelement = 1; secondelement <= n;secondelement++) { int first = array[j]; int second = array[secondelement]; secondeleme
int max_dist = 0;
for (int j= 0; j < n; j++)
{
for (int secondelement = 1; secondelement <= n;secondelement++)
{
int first = array[j];
int second = array[secondelement];
secondelement++;
if (first == second)
{
max_dist = max_dist +1;
System.Console.WriteLine(first+""+max_dist);
first = second;
break;
}
else
{
break;
}
}
}
2 3 5 7 3
out put is
3 3
2757334567
out put is
7 4
13571
out put is
1 3
它的平均数x最长距离是y,再次出现
注意:这个数字可能会被发现很多次您的第一个错误是在循环中增加了第二个元素。第二个错误是增加max_dist,而不是计算索引之间的差异。在第二个循环的每次迭代中计算第一个也是毫无意义的,您应该从第一个循环的当前索引的下一个开始内部循环。打破这种循环也是毫无意义的 编辑: 让我们修复您的代码:
for (int j= 0; j < n; j++)
{
int max_dist = 0;
int first = array[j];
for (int secondelement = j + 1; secondelement < n;secondelement++)
{
int second = array[secondelement];
if (first == second)
{
if (max_dist < secondelement - j)
{
max_dist = secondelement - j;
}
first = second;
}
}
System.Console.WriteLine(first+" "+max_dist);
}
for(int j=0;j
编辑:
您还需要避免重复输出:
for (int j= 0; j < n; j++)
{
int max_dist = 0;
int first = array[j];
int earlierIndex = -1;
for (int k = 0; (earlierIndex < 0) && (k < j); k++) {
if (first == array[k]) {
earlierIndex = k;
}
}
if (earlierIndex >= 0) continue;
for (int secondelement = j + 1; secondelement < n;secondelement++)
{
int second = array[secondelement];
if (first == second)
{
if (max_dist < secondelement - j)
{
max_dist = secondelement - j;
}
first = second;
}
}
System.Console.WriteLine(first+" "+max_dist);
}
for(int j=0;j=0)继续;
对于(int secondelement=j+1;secondelement
您的第一个错误是在循环中增加第二个元素。第二个错误是增加max_dist,而不是计算索引之间的差异。在第二个循环的每次迭代中计算第一个也是毫无意义的,您应该从第一个循环的当前索引的下一个开始内部循环。打破这种循环也是毫无意义的
编辑:
让我们修复您的代码:
for (int j= 0; j < n; j++)
{
int max_dist = 0;
int first = array[j];
for (int secondelement = j + 1; secondelement < n;secondelement++)
{
int second = array[secondelement];
if (first == second)
{
if (max_dist < secondelement - j)
{
max_dist = secondelement - j;
}
first = second;
}
}
System.Console.WriteLine(first+" "+max_dist);
}
for(int j=0;j
编辑:
您还需要避免重复输出:
for (int j= 0; j < n; j++)
{
int max_dist = 0;
int first = array[j];
int earlierIndex = -1;
for (int k = 0; (earlierIndex < 0) && (k < j); k++) {
if (first == array[k]) {
earlierIndex = k;
}
}
if (earlierIndex >= 0) continue;
for (int secondelement = j + 1; secondelement < n;secondelement++)
{
int second = array[secondelement];
if (first == second)
{
if (max_dist < secondelement - j)
{
max_dist = secondelement - j;
}
first = second;
}
}
System.Console.WriteLine(first+" "+max_dist);
}
for(int j=0;j=0)继续;
对于(int secondelement=j+1;secondelement
如果要计算的数字超过1个
var myArray = new[] { 1, 2, 3, 4, 5, 6, 4, 8, 9, 0, 4, 3, 8 }; // 4, 3 and 8
var numbers = myArray.GroupBy(x=>x).Where(x => x.Count() > 1).Select(x => x.Key).ToList(); // Selects 4, 3, 8
foreach (var n in numbers)
{
var first = Array.IndexOf(myArray, n); //get first occurence of each number
var last = Array.LastIndexOf(myArray, n); //get last occurence of each number
var dist = last - first; //calculate distance
Console.WriteLine($"Number: {n}, distance: {dist}"); //print distance
}
如果要计算的数字超过1个
var myArray = new[] { 1, 2, 3, 4, 5, 6, 4, 8, 9, 0, 4, 3, 8 }; // 4, 3 and 8
var numbers = myArray.GroupBy(x=>x).Where(x => x.Count() > 1).Select(x => x.Key).ToList(); // Selects 4, 3, 8
foreach (var n in numbers)
{
var first = Array.IndexOf(myArray, n); //get first occurence of each number
var last = Array.LastIndexOf(myArray, n); //get last occurence of each number
var dist = last - first; //calculate distance
Console.WriteLine($"Number: {n}, distance: {dist}"); //print distance
}
我编辑了我的问题我现在看到了编辑我编辑了我的问题我现在看到了edit@Yasmeen在哪一行?还有,你的数组中有多少个项目?我让我的数组是1,2,3,4,1In int second=当我移除或等于ty时,第二个元素的数组lineWorkedmuch@Yasmeen不客气,我将编辑我的答案以添加这些信息。@Yasmeen在哪一行?还有,你的数组中有多少个项目?我让我的数组是1,2,3,4,1In int second=当我移除或等于ty时,第二个元素的数组lineWorkedmuch@Yasmeen不客气,我会编辑我的答案来添加这些信息。