2016-11-09 125 views
0

可以说我有小行星对象的列表,像这样:保持跟踪

  • 9_Amphitrite
  • 24_Themis
  • 259_Aletheia
  • 31_Euphrosyne
  • 511_Davida
  • 87_Sylvia
  • 9_Metis
  • 41_Daphne

每个小行星有冠军,一个StartRoationPeriodEndRoationPeriod

我需要根据当前小行星StartRoationPeriod和前一个小行星EndRoationPeriod与轨道常数的接近程度来连接它们的名称,然后吐出连接标题。

因此,与上述名单,最终的对象可能是这样的:

  • 9_Amphitrite
  • 24_Themis; 259_Aletheia
  • 31_Euphrosyne; 511_Davida; 87_Sylvia
  • 9_Metis
  • 41_Daphne

这需要我跟踪的当前和以前的小行星。

我开始写循环,但我不确定在哪里或甚至如何检查当前小行星开始旋转周期对前小行星结束旋转周期...基本上,它只是得到凌乱...

 string asteroid_title = string.Empty; 
     Asteroid prev_asteroid = null; 

     foreach (var asteroid in SolarSystem) 
     { 
      if (prev_asteroid != null) 
      { 
       if (asteroid.StartRoationPeriod + OrbitalConstant >= prev_asteroid.EndRoationPeriod) 
       { 
         asteroid_title = asteroid_title + asteroid.Title; 

       } else { 
         asteroid_title = asteroid.Title; 
         yield return CreateTitle(); 
       } 
      } 
      prev_evt = evt; 
     } 

回答

1

我认为这应该为你工作(如果总看起来太复杂尝试将其转换为一个foreach,很容易)

using System; 
using System.Collections.Generic; 
using System.Linq; 

namespace Program 
{ 
    class Asteroid 
    { 
     public int EndRoationPeriod { get; internal set; } 
     public string Name { get; internal set; } 
     public int StartRoationPeriod { get; internal set; } 
    } 

    class AsteroidGroup 
    { 
     public int EndRoationPeriod { get; internal set; } 
     public string Names { get; internal set; } 
    } 
    internal class Program 
    { 
     private static void Main(string[] args) 
     { 
      int OrbitalConstant = 10; 
      List<Asteroid> SolarSystem = new List<Asteroid>() 
      { 
       new Asteroid() { Name= "9_Amphitrite" ,StartRoationPeriod=10 ,EndRoationPeriod=50}, 
       new Asteroid() { Name= "24_Themis" ,StartRoationPeriod=45,EndRoationPeriod=100}, 
       new Asteroid() { Name= "259_Aletheia",StartRoationPeriod=40 ,EndRoationPeriod=150}, 
       new Asteroid() { Name= "31_Euphrosyne" ,StartRoationPeriod=60,EndRoationPeriod=200}, 
       new Asteroid() { Name= "511_Davida" ,StartRoationPeriod=195,EndRoationPeriod=250}, 
       new Asteroid() { Name= "87_Sylvia" ,StartRoationPeriod=90,EndRoationPeriod=300}, 
       new Asteroid() { Name= "9_Metis" ,StartRoationPeriod=100,EndRoationPeriod=350}, 
       new Asteroid() { Name= "41_Daphne" ,StartRoationPeriod=110,EndRoationPeriod=400}, 
      }; 

      var result = //I skip the first element because I initialize a new list with that element in the next step 
          SolarSystem.Skip(1) 
          //The first argument of Aggregate is a new List with your first element 
          .Aggregate(new List<AsteroidGroup>() { new AsteroidGroup { Names = SolarSystem[0].Name, EndRoationPeriod = SolarSystem[0].EndRoationPeriod } }, 
          //foreach item in your list this method is called,l=your list and a=the current element  
          //the method must return a list           
          (l, a) => 
          { 
          //Now this is your algorithm 
          //Should be easy to undrestand 

           var last = l.LastOrDefault(); 
           if (a.StartRoationPeriod + OrbitalConstant >= last.EndRoationPeriod) 
           { 
            last.Names += " " + a.Name; 
            last.EndRoationPeriod = a.EndRoationPeriod; 
           } 
           else 
            l.Add(new AsteroidGroup { Names = a.Name, EndRoationPeriod = a.EndRoationPeriod }); 

           //Return the updated list so it can be used in the next iteration 
           return l; 
          }); 

一个更紧凑的解决方案

var result = SolarSystem 
      .Skip(1) 
      .Aggregate(SolarSystem.Take(1).ToList(), 
         (l, a) => (a.StartRoationPeriod + OrbitalConstant >= l[l.Count - 1].EndRoationPeriod) ? 
         (l.Take(l.Count - 1)).Concat(new List<Asteroid> { new Asteroid() { Name = l[l.Count - 1].Name += " " + a.Name, EndRoationPeriod = a.EndRoationPeriod } }).ToList() : 
          l.Concat(new List<Asteroid> { a }).ToList() 
         ); 
+0

哇,这是很多的代码。 (1)和()做什么,线(l,a)=>做什么?谢谢! – SkyeBoniwell

+0

@SkyeBoniwell添加了一些评论,忽略了紧凑代码,这仅仅是为了乐趣 –

+0

感谢您的评论。什么是变量,结果?它只是一个字符串?谢谢 – SkyeBoniwell