查找数字的唯一分区

时间:2016-12-10 22:53:55

标签: unique

我使用下面的代码来查找给定数字(N)的分区。我怎样才能确保只有独特的分区?例如,分区(1,1,1,7),(1,1,7,1),(1,7,1,1)和(7,1,1,1)将被视为相同且仅一个这些应该输出。

由于

此致

 Dim N = 10

        For i As Integer = 0 To N
            For j As Integer = 0 To N
                For k As Integer = 0 To N
                    For l As Integer = 0 To N
                        If i + j + k + l = N Then
                            Dim St As String = String.Format("({0:d}, {1:d}, {2:d}, {3:d})", i, j, k, l)
                            Console.WriteLine(St)
                        End If
                    Next
                Next
            Next
        Next

        Console.Read()
编辑:以下似乎是根据某人的建议开展工作;

Module Module1

    Sub Main()

        Console.WriteLine("Please enter an integer.")
        Dim sReadLine As String = Console.ReadLine()
        Dim iValue As Integer

        If IsNumeric(sReadLine) Then
            iValue = CInt(sReadLine)
        Else
            Console.WriteLine("'" & sReadLine & "' is not a numeric value. Press any key to exit.")
            'Application.Exit()
            Console.Read()
            Exit Sub
            End
        End If

        Console.Clear()
        Console.WriteLine("Number is {0}", iValue)
        Console.WriteLine("")

        Partitions1(iValue)

        Exit Sub

    End Sub

    Dim partitions As New List(Of Part)

    Private Sub Partitions1(N As Integer)
        For i As Integer = 0 To N
            For j As Integer = 0 To N
                For k As Integer = 0 To N
                    For l As Integer = 0 To N
                        If i + j + k + l = N Then
                            Dim thisPartition As New Part()
                            thisPartition.Parts = New Integer() {i, j, k, l}
                            If Not partitions.Contains(thisPartition) Then
                                partitions.Add(thisPartition)
                            End If
                        End If
                    Next
                Next
            Next
        Next

        For Each x In partitions
            Dim St = "("
            For Each y In x.Parts
                St = St & y & ", "
            Next
            St = Left(St, Len(St) - 2)
            St = St & ")"
            Console.WriteLine(St)
        Next

        Console.WriteLine("")
        Console.WriteLine("{0} unique partititons found.", partitions.Count)
        Console.Read()

    End Sub

    Public Class Part 'Sorted array of integer with comparer
        Implements IEquatable(Of Part)
        Public Property Parts As Integer()
            Get
                Return m_Parts
            End Get
            Set(value As Integer())
                m_Parts = value
                Array.Sort(m_Parts)
            End Set
        End Property

        Private m_Parts As Integer()

        Public Overloads Function Equals(other As Part) As Boolean _
           Implements IEquatable(Of Part).Equals
            If other Is Nothing Then
                Return False
            End If
            If other.Parts.GetLength(0) <> m_Parts.GetLength(0) Then Return False
            Dim result As Boolean = True
            Array.Sort(other.Parts)
            For I As Integer = 0 To other.Parts.GetLength(0) - 1
                If other.Parts(I) <> m_Parts(I) Then
                    result = False
                    Exit For
                End If
            Next
            Return result
        End Function
        ' Should also override == and != operators.
    End Class

End Module

1 个答案:

答案 0 :(得分:0)

在列表中收集找到的解决方案,然后编写一个算法,从列表中删除重复项,然后打印列表。