如何从存储sum的数组中查找并显示重复的整数

时间:2018-04-21 10:08:33

标签: c#

我想显示具有相同结果的竞争者,例如,新阵列的索引3,4,6具有相同的总和。在这种情况下,我应该显示像#34;竞争者4,竞争者6,竞争者7是最高的得分7"。

但是,我的代码仅显示竞争对手4,其中得分最高,为7分。

int[] newarray = new newarray{4, 6, 2, 7, 7, 2, 7}


public static void Compare_competitor(int[] newarray) {

    /// Create a new valiable for highest results.
    int Max_score_of_highest_competitor = newarray[0];

    /// Create a new valiable for finding index of a competitor who is highest.
    int indexofmax = newarray[0];

    /// Use for loop to find out highest competitor from newarray.
    for (int index = 0; index < newarray.Length; index++) {
        if ((Max_score_of_highest_competitor < newarray[index])) {
            /// Exchange values between tmp and Max_score_of_highest_competitor.
            int tmp = Max_score_of_highest_competitor;
            Max_score_of_highest_competitor = newarray[index];
            newarray[index] = Max_score_of_highest_competitor;

            /// This is a index of highest competitor.
            indexofmax = index;
        }                               

    }

    /// Show users a highest competitor with score.    
    Console.WriteLine("\nAnd the winner is competitor{0}", indexofmax + 1);
    Console.WriteLine("with total scores of {0}", Max_score_of_highest_competitor);
}

4 个答案:

答案 0 :(得分:2)

我在Compare_competitor方法中实现了LINQ解决方案:

public static void Compare_competitor(int[] newarray)
{
    var indexes = Enumerable.Range(0, newarray.Length)
                            .Where(i => newarray[i] == newarray.Max())
                            .Select(i => i);

    List<string> winners = new List<string>();
    foreach (var item in indexes)
    {
        winners.Add(String.Format("Competitor {0}", item + 1));
    }
    Console.WriteLine("{0} are the highest with score of {1}", 
                      string.Join(", ", winners), newarray.Max());
}

Enumerable.Range(0, newarray.Length)生成指定范围内的整数序列,例如0newarray.Length-10,1,2..6)。此序列表示数组元素的索引。 Where子句过滤等于newarraynewarray.Max())中最高元素的元素的索引。Select子句然后将这些索引存储在变量{{ 1}}。

方法调用:

indexes

输出:竞争对手4,竞争对手5,竞争对手7最高,得分为7

DEMO HERE

答案 1 :(得分:0)

你可以试试这个

:os.cmd('rm /path/to/dir/#{uuid}-frames-*.png')

答案 2 :(得分:0)

试试这个:

static void Main(string[] args)
{
    int[] newarray = new int[] { 4, 6, 2, 7, 7, 2, 7 };
    Console.WriteLine(Compare_competitor(newarray));
    Console.ReadKey();
}

public static string Compare_competitor(int[] newarray)
{
    string message = "";
    //find max. value, then iterate through array and add "Competitor" + index to the message, if has max. score
    int max = newarray.Max();
    for(int i = 0; i < newarray.Length; i++)
        message += newarray[i] == max ? "Competitor " + (i + 1) + ", " : "";
    //remove last comma and space from current string
    message = message.Remove(message.Length - 2);
    return message + " are the highest with score " + max;
}

答案 3 :(得分:0)

试试这个:

int[] newarray = new[] { 4, 6, 2, 7, 7, 2, 7 };

var highest = newarray.Select((x, n) => new { x, n }).GroupBy(x => x.x).OrderByDescending(x => x.Key).First();

var result = String.Join(", ", highest.Select(h => $"Competitor {h.n + 1}")) + $" are the highest with {highest.Key}";

这给出了:

  

竞争对手4,竞争对手5,竞争对手7最高,为7