2014-06-30 31 views
1

我想要做的是在兩側得到1到20之間的直角三角形的數量。C#比較列表對象和傳遞的參數

大部分的邏輯是好的,但是當我想檢查3,4和5時,這是一個三角形,而4,3和5將不是有效的,因爲它是3,4,5以不同的順序。

這裏是我剛纔的情況下,任何人的問題區域

public bool isAlreadyValidTriangle(int intAdj, int intOpp, List<Triangle> triangleList) 
{ 
    bool breakLoop = false; 
    Int32 length = triangleList.Count; 
    for (int index = 0; index < length && breakLoop != false; index++) 
    { 
     //This is to compare an existing adjacent that is stored in the list to the 
     //supplied opposite, this is to prebent the 3, 4, 5 and 4, 3, 5 issue 
     var response = triangleList.Find(r => r.IntAdjacent == intOpp); 

     if (response !=null) 
     { 
      //This is to compare an existing opposite that is stored in the list to the 
      //supplied adjacent, this is to prebent the 3, 4, 5 and 4, 3, 5 issue 
      var otherResponse = triangleList.Find(r => r.IntOpposite == intAdj); 

      if (otherResponse != null) 
      { 
       breakLoop = true; 
      } 
     } 
    } 
    return breakLoop; 
} 

書面需要三角碼的代碼,這裏是

public class Triangle 
{ 
    private int intAdjacent; 
    private int intOpposite; 
    private int intHypotenuse; 

    public Triangle(int intAdjacent, int intOpposite, int intHypotenuse) 
    { 
     this.intAdjacent = intAdjacent; 
     this.intOpposite = intOpposite; 
     this.intHypotenuse = intHypotenuse; 
    } 

    public int IntAdjacent 
    { 
     get { return intAdjacent; } 
    } 

    public int IntOpposite 
    { 
     get { return intOpposite; } 
    } 

    public int IntHypotenuse 
    { 
     get { return intHypotenuse; } 
    } 
} 

可能有人當場看到我我在邏輯中犯了錯誤還是在代碼本身中犯了錯誤?如果他們不這樣做

public bool isAlreadyValidTriangle(int intAdj, int intOpp, List<Triangle> triangleList) 
{ 
    if(triangleList.Any(t => t.IntAdjacent == intAdj && t.IntOpposite == intOpp)) 
     return true; 

    return triangleList.Any(t => t.IntAdjacent == intOpp && t.IntOpposite == intAdj); 

} 

它首先對於其中傳遞的值是符合的比賽,然後反轉搜索: 基思

+0

「我想要做的是在兩側獲得1到20之間的直角三角形的數量。」回頭看看你自己,並認爲一個三角形有三面。這些都不能通過術語「兩者」來解決。您是否在尋找所有面短於20的三角形?列表不是無限的嗎? – spender

+0

你能否澄清你的目標究竟是什麼,你期待什麼結果以及實際問題是什麼? – vesan

+0

由於您的方法有點不對,代碼無法正確工作。說這個,因爲你正在搜索整個列表,可能包含許多帶有值加載的對象,所以Find方法在任何找到它的地方返回一個值,而不是在特定的索引中。您必須改爲比較列表中每個特定索引的值。 – Transcendent

回答

2

您可以簡化這一這樣頗多。它與你的代碼略有不同,它同時尋找相鄰和相反的地方,這是你出錯的地方。此外,它使用Any,如果找到任何匹配的項目,它將返回一個布爾值。

思考這個進一步,我會改變的功能,使它的擴展方法是這樣的:

public static bool isAlreadyValidTriangle(this List<Triangle> triangleList, int intAdj, int intOpp) 
{ 
    if(triangleList.Any(t => t.IntAdjacent == intAdj && t.IntOpposite == intOpp)) 
     return true; 

    return triangleList.Any(t => t.IntAdjacent == intOpp && t.IntOpposite == intAdj); 

} 

這意味着你可以多一點的可讀性稱之爲:

List<Triangle> triangleList = new List<Triangle>(); 
... fill list with triangles ... 

if(triangleList.isAlreadyValidTriangle(adjacent, opposite) 
{ 
    ... 
} 
+1

@Intercendent我不明白你在說什麼。我的代碼不同,因爲它同時比較相鄰和相反。 – DavidG

+0

抱歉誤解了你的代碼,沒錯,我沒有注意到'&&' – Transcendent

0

第一非常感謝您的建議和幫助。

這裏是從開始的完整代碼完成

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Collections; 

    namespace ConsoleApplication1 
    { 
    public class Program 
    { 
    private static double doubleHypotenuse = 0; 
    private static int adjacent = 1; 
    private static int opposite = 1; 
    private static int limit = 200; 
    private static int count = 0; 

    public static void Main(string[] args) 
    { 

     TriangleLogic triLogic = new TriangleLogic(); 
     List<Triangle> triangleList = new List<Triangle>(); 
     List<Triangle> trianglePlus1 = new List<Triangle>(); 

     while (adjacent < limit) 
     { 
      opposite = 1; 
      while (opposite < limit) 
      { 
       doubleHypotenuse = triLogic.intRightAngle(adjacent, opposite); 
       if (doubleHypotenuse % 1 == 0) 
       { 
        if (!triLogic.isAlreadyValidTriangle(adjacent, opposite, triangleList)) 
        { 
         triangleList.Add(new Triangle(adjacent, opposite, (int)Convert.ToInt32(doubleHypotenuse))); 
        } 

        count++; 
       } 
       opposite++; 
      } 
      adjacent++; 
     } 

     Console.WriteLine("The following are integer triangles"); 
     triangleList.ForEach(delegate(Triangle pytag) 
     { 
      if ((pytag.IntHypotenuse - pytag.IntOpposite) == 1) 
      { 
       trianglePlus1.Add(new Triangle(pytag.IntAdjacent, pytag.IntOpposite, pytag.IntHypotenuse)); 
      } 
      Console.WriteLine(pytag.IntAdjacent + ", " + pytag.IntOpposite + " and " + pytag.IntHypotenuse); 
     }); 

     Console.WriteLine("the number of squares is " + count); 
     Int32 length = triangleList.Count; 
     Console.WriteLine("the length of the list is " + length); 

     Console.WriteLine(""); 
     Console.WriteLine("the List of triangles with the hypotenuse 1 "); 
     Console.WriteLine("more than the opposite"); 

     trianglePlus1.ForEach(delegate(Triangle pytagPlus1) 
     { 
      Console.WriteLine(pytagPlus1.IntAdjacent + ", " + pytagPlus1.IntOpposite + " and " + pytagPlus1.IntHypotenuse); 
     }); 


     Int32 lengthPlus1 = trianglePlus1.Count; 
     Console.WriteLine("the length of the list is " + lengthPlus1); 

    } 
} 
} 

這裏是三角類

public class Triangle 
    { 
    private int intAdjacent; 
    private int intOpposite; 
    private int intHypotenuse; 

    public Triangle(int intAdjacent, int intOpposite, int intHypotenuse) 
    { 
     this.intAdjacent = intAdjacent; 
     this.intOpposite = intOpposite; 
     this.intHypotenuse = intHypotenuse; 
    } 

    public int IntAdjacent 
    { 
     get { return intAdjacent; } 
    } 

    public int IntOpposite 
    { 
     get { return intOpposite; } 
    } 

    public int IntHypotenuse 
    { 
     get { return intHypotenuse; } 
    } 
} 

最後的TriangleLogic類再次

public class TriangleLogic 
    { 
    private double squareAdjacent = 0; 
    private double squareOpposite = 0; 
    private double squareSum = 0; 

    public TriangleLogic() 
    { 

    } 

    public double intRightAngle(int intAdjacent, int intOpposite) 
    { 
     squareAdjacent = Math.Pow(Convert.ToDouble(intAdjacent), 2); 
     squareOpposite = Math.Pow(Convert.ToDouble(intOpposite), 2); 
     squareSum = squareAdjacent + squareOpposite; 
     return Math.Sqrt(squareSum); 
    } 

    public bool isAlreadyValidTriangle(int intAdj, int intOpp, List<Triangle> triangleList) 
    { 
     if (triangleList.Any(t => t.IntAdjacent == intAdj && t.IntOpposite == intOpp)) 
      return true; 

     return triangleList.Any(t => t.IntAdjacent == intOpp && t.IntOpposite == intAdj); 
    } 
} 

多謝支持

+0

我對我的答案做了另一個編輯。我知道你已經接受,但它一直在竊聽我,我不得不做出改變:) – DavidG