输入一组大于0的整数,删除其中所有重复出现的字,并要求满足如下条件:
- 越大的数最终位置尽可能靠前
- 优先满足较大的数靠前
例1
1, 5, 4, 9, 4, 3, 4, 8, 7, 6, 9, 8, 7, 1
1, 5, 4, 9, 4, 3, 4, 8, 7, 6, 9, 8, 7, 1
例2
8, 4, 7, 6, 4, 9, 7, 9
8, 4, 7, 6, 4, 9, 7, 9
实现:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
class Program
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//var numbers = new int[] { 8, 4, 7, 6, 4, 9, 7, 9 };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var numbers = new int[]
{ 1, 5, 4, 9, 4, 3, 4, 8, 7, 6, 9, 8, 7, 1 };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int index = 0;
var groups = from n in numbers
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
let i = new NumItem
{ Value = n, Index = index++, Cost = 0 }
group i by i.Value into g
orderby g.Key descending
select g;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var destNumbers = new List<NumItem>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (var valueGroup in groups)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var maxCostItem = valueGroup.First();
foreach (var num in valueGroup)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num.Cost = destNumbers.Count(i => i.Index < num.Index && i.Value > num.Value);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (maxCostItem.Cost < num.Cost)
maxCostItem = num;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
destNumbers.Add(maxCostItem);
}
destNumbers.Sort((i1, i2) => i1.Index - i2.Index);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine(string.Join(" ",
destNumbers.Select(i => i.Value.ToString()).ToArray()));
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class NumItem
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Value
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Index
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Cost
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override string ToString()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Index + " " + Value + " " + Cost;
}
}
代码比较简单,但写出这段代码还是费了点功夫,有兴趣的朋友可以自己练习一下。