-3

我有一个带有属性的 StationInfo 类:

public uint EnergyConsumption { get; private set; }
public uint CurrentEnergyAmount { get; private set; }
public uint NumRobotsNearStation { get; private set; }
public uint NumStationsAround { get; private set; }

我正在创建一个 StationInfo 类型的数组。

在这个数组中,我需要找到一个同时满足以下条件的对象:

EnergyConsumption -> min
CurrentEnergyAmount -> max
NumRobotsNearStation -> min
NumStationsAround -> max

带有 2 个参数的示例:

{2,1}
{3,4}
{6,7}
{1,10}
{2, 15}

需要找到{min, max}

没有具有此条件的对象,但存在与 min 和 max 值接近的对象{2, 15}

有 C# 交钥匙解决方案来解决这个问题吗?

4

4 回答 4

0

怎么样:

var filtered = myArray.Where(a => a.EnergyConsumption == min 
   && a.CurrentEnergyAmount == max
   && a.NumRobotNearStation == max
   && a.NumStationsAround == min)
于 2013-09-20T16:26:12.193 回答
0

一个电台不可能同时满足所有这些标准。返回所有满足至少一项标准的电台可能更有意义。

uint minEnergyConsumption = stations.Min(s => s.EnergyConsumption);
uint maxCurrentEnergyAmount = stations.Max(s => s.CurrentEnergyAmount);
uint maxNumRobotsNearStation = stations.Max(s => s.NumRobotsNearStation);
uint minNumStationsAround = stations.Min(s => s.NumStationsAround);

var result = stations
    .Where(s => s.EnergyConsumption == minEnergyConsumption ||
                s.CurrentEnergyAmount == maxCurrentEnergyAmount ||
                s.NumRobotNearStation == maxNumRobotsNearStation ||
                s.NumStationsAround == minNumStationsAround);
于 2013-09-20T16:38:06.917 回答
0

您可以根据自己的条件创建自己的比较器进行排序:

stations.Sort( new StationsComparer() );

其中 StationsComparer 是:

class StationsComparer: IComparer<StationInfo>
{
    public int Compare( StationInfo x, StationInfo y )
    {
        int result = x.EnergyConsumption.CompareTo( y.EnergyConsumption );

        if( result == 0 )
            result = y.CurrentEnergyAmount.CompareTo( x.CurrentEnergyAmount );

        if( result == 0 )
            result = y.NumRobotNearStation.CompareTo( x.NumRobotNearStation );

        if( result == 0 )
            result = x.NumStationsAround.CompareTo( y.NumStationsAround );

        return result;
    }
}
于 2013-09-20T17:15:44.217 回答
0

我认为这个 2-pass 解决方案非常适合您。

  • 1遍找到每个有趣属性的最小值
  • 1 次通过以找到匹配的项目(如果有)

    StationInfo FindMin( IEnumerable<StationInfo> stations )
    {
      uint minEnergyConsumption    = uint.MaxValue ;
      uint maxCurrentEnergyAmount  = uint.MinValue ;
      uint minNumRobotsNearStation = uint.MaxValue ;
      uint maxNumStationsAround    = uint.MinValue ;
    
      // find the min value for each interesting field
      foreach ( StationInfo si in stations )
      {
        minEnergyConsumption    = si.EnergyConsumption    < minEnergyConsumption    ? si.EnergyConsumption    : minEnergyConsumption    ;
        maxCurrentEnergyAmount  = si.CurrentEnergyAmount  > maxCurrentEnergyAmount  ? si.CurrentEnergyAmount  : maxCurrentEnergyAmount  ;
        minNumRobotsNearStation = si.NumRobotsNearStation < minNumRobotsNearStation ? si.NumRobotsNearStation : minNumRobotsNearStation ;
        maxNumStationsAround    = si.NumStationsAround    > maxNumStationsAround    ? si.NumStationsAround    : maxNumStationsAround    ;
      }
    
      StationInfo instance = stations.FirstOrDefault( x =>
           x.EnergyConsumption    == minEnergyConsumption 
        && x.CurrentEnergyAmount  == maxCurrentEnergyAmount
        && x.NumRobotsNearStation == minNumRobotsNearStation
        && x.NumStationsAround    == maxNumStationsAround
      );
      return instance ;
    }
    
于 2013-09-20T17:15:56.350 回答