Skip to content

Instantly share code, notes, and snippets.

@joelclermont
Created April 7, 2011 02:23
Show Gist options
  • Save joelclermont/906906 to your computer and use it in GitHub Desktop.
Save joelclermont/906906 to your computer and use it in GitHub Desktop.
naive algorithm
Imports System.IO
Module Module1
Sub Main()
Dim startTime As DateTime = Now
Dim N As Integer 'number of test cases
Dim C As Integer 'amount of credit
Dim l As Integer 'number of items in store
Dim P As List(Of Integer) 'cost of item
Dim answer() As Integer
'parse the file
Dim inputName As String = "c:\users\joel\documents\visual studio 2010\Projects\cj-storecredit\A-small.in"
Dim inputStream As StreamReader = New StreamReader(inputName)
Dim outputName As String = "c:\users\joel\documents\visual studio 2010\Projects\cj-storecredit\A-small.out"
Dim outputStream As StreamWriter = New StreamWriter(outputName, False)
N = CInt(inputStream.ReadLine)
For caseNumber As Integer = 1 To N
C = CInt(inputStream.ReadLine)
l = CInt(inputStream.ReadLine)
P = inputStream.ReadLine.Split(" ").ToList().ConvertAll(Function(i) CInt(i))
answer = SolveLine(C, P)
outputStream.WriteLine(String.Format("Case #{0}: {1} {2}", caseNumber, answer(0), answer(1)))
Next
outputStream.Close()
Dim runLength As Global.System.TimeSpan = Now.Subtract(startTime)
Dim millisecs As Integer = runLength.TotalMilliseconds
Console.WriteLine(millisecs)
Console.ReadLine()
End Sub
Public Function SolveLine(ByVal C As Integer, ByVal P As List(Of Integer)) As Integer()
Dim lowItem, highItem As Integer
For outer As Integer = 0 To P.Count - 1
For inner As Integer = outer + 1 To P.Count - 1
If P(outer) + P(inner) = C Then
lowItem = outer + 1
highItem = inner + 1
Exit For
End If
Next
If lowItem <> Nothing Then Exit For
Next
Return New Integer() {lowItem, highItem}
End Function
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment