Springen naar inhoud


- - - - -
Solved

Opdracht 022



  • Log in a.u.b. om te beantwoorden
Er zijn 2 reacties in dit onderwerp

#1 Xymtrix

Xymtrix

    Master Developer

  • Leden
  • PipPipPipPipPip
  • 533 berichten
    Laatst bezocht 13 aug 2012 15:48
  • LocatieNederland

Geplaatst op 15 oktober 2010 - 09:49

Citeren

Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order.
Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list.
So, COLIN would obtain a score of 938  53 = 49714.
What is the total of all the name scores in the file?


#2 Xymtrix

Xymtrix

    Master Developer

  • Leden
  • PipPipPipPipPip
  • 533 berichten
    Laatst bezocht 13 aug 2012 15:48
  • LocatieNederland

Geplaatst op 15 oktober 2010 - 09:49

Visual Basic Code:
 Function Problem22() As Long
		Dim Answer As Long = 0
		Dim Name As Array
		Dim NameValue As Integer = 0

		Using sr As New IO.StreamReader(New IO.FileStream("c:\names.txt", IO.FileMode.Open, IO.FileAccess.Read))
			Dim Line As String
			Line = sr.ReadLine.Replace("""", "")
			Name = Line.Split(",")
		End Using

		Array.Sort(Name)

		For I As Integer = 0 To UBound(Name)
			NameValue = 0
			For Each Lettre As String In Name(I).ToString.ToCharArray
				NameValue += CInt(Asc(Lettre)) - 64
			Next
			NameValue *= (I + 1)
			Answer += NameValue
		Next

		Return Answer

	End Function
Antwoord verschijnt meteen in beeld.

#3 josk79

josk79

    Master Developer

  • Leden
  • PipPipPipPipPip
  • 614 berichten
    Laatst bezocht 16 aug 2018 17:25

Geplaatst op 15 oktober 2010 - 21:55

Dit is mijn versie. Eigenlijk bijna identiek:

Visual Basic Code:
	Public Class Problem022
		Implements IEulerSolution

		Public Event OnDebugMessage(ByVal str As String) Implements Module1.IEulerSolution.OnDebugMessage

		Public Function Solve() As String Implements Module1.IEulerSolution.Solve
			Dim names() As String
			Using infile As System.IO.StreamReader = New System.IO.StreamReader("c:\names.txt")
				names = infile.ReadLine.Replace("""", "").Split(","c)
			End Using

			Array.Sort(names)
			Dim total As Long = 0

			For i As Integer = 0 To names.Count - 1
				total += GetNameScore(names(i)) * (i + 1)
			Next

			Return total.ToString
		End Function

		Public Function GetNameScore(ByVal name As String) As Integer
			'Verkrijg naamscore van de opgegeven naam (moet hoofdletters zijn)
			Dim namescore As Integer = 0
			For Each cha As Char In name
				namescore += Asc(cha) - 64
			Next
			Return namescore
		End Function
	End Class







Ook met taq Solved voorzien

0 gebruiker(s) lezen dit onderwerp

0 lid(leden), 0 bezoeker(s), 0 anonieme gebruikers

Inloggen


[Solved] Untitled 1

Met dank aan Jürgen voor de jarenlange inzet van visualbasic.be (anno dec 2000)
Met dank aan Mike en Ronneke voor de jarenlange inzet van vbib.be (anno dec 2010)
Met dank aan PascalBianca voor de jarenlange inzet van vbib.be (anno dec 2016)