1

私は、講師がレポートを採点できるソフトウェアの開発に取り組んでおり、講師が 3 つの連続したレポートに同じ成績を付けているかどうかを確認する必要があります。基本的に、私は成績のリストを持っています:

80,81,90,90,90,100,85,86,86,79,95,95,95

このリストで 90 年代と 95 年代を特定する必要があります (それぞれ連続して 3 回与えられています)。

追伸 - 私の投稿に「宿題」のフラグを立て続けている人のために、私が生徒と成績を扱っているからといって、それが教室の課題であるとは限りません。ジミニー。

4

4 に答える 4

7

You can do this quite easily with looping through over i from 2 to list.Length and check if list[i] == list[i - 1] && list[i - 1] == list[i - 2].

For instance, it can be written like this:

var list = new[] { 80,81,90,90,90,100,85,86,86,79,95,95,95 };
var dupes = new List<int>();
for(var i = 2; i < list.Length; i++) {
    if(list[i] == list[i - 1] && list[i] == list[i - 2])
        dupes.Add(list[i]);
}

[Edit]
Here's a running example: http://ideone.com/UGwFwq

If you would not like double reported when there are 4 (or more) equals in a row, a good way to prevent that from happening is simply keeping a temp variable on the previous "3 in a row" was found and check against that before you append to the dupes list.

Something like this:

var list = new[] { 80,81,90,90,90,90,90,90,100,85,86,86,79,95,95,95,95 };
var dupes = new List<int>();
int? prev = null;
for(var i = 2; i < list.Length; i++) {
    if(list[i] == list[i - 1] && list[i] == list[i - 2]) {
        if(!prev.HasValue || prev.Value != list[i]) {
            dupes.Add(list[i]);
            prev = list[i];
        }
    }
}

Showcase: http://ideone.com/jbokMQ

[Edit 2]
And if for some reason you need to run this in a LINQ-like manner (for instance if you have a HUGE dataset or a stream of data and you want to run this in a lazy manner), a solution to that can be found here: http://ideone.com/R1ZBVk

于 2013-11-11T20:49:56.040 に答える
0

これを試すことができます:

int intDuplicateCriteria = 2; //Any grade duplicated by a value greater than 2 
int[] lstGrades = new[] { 80, 81, 90, 90, 90, 100, 85, 86, 86, 79, 95, 95, 95 };

var lstDuplicate = lstGrades
                 .GroupBy(grade => grade)
                 .Where(g => g.Count() > intDuplicateCriteria)
                 .Select(g => g.Key);


//Display consecutively grades given 3 times
foreach (var d in lstDuplicate)
    {
      Console.WriteLine(d.ToString());               
    }
于 2013-11-11T21:29:44.950 に答える