2016-11-09 162 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