Created
May 25, 2013 11:46
-
-
Save regularcoder/5648821 to your computer and use it in GitHub Desktop.
C# solution for 'Store Credit' from code jam 2010
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Created by SharpDevelop. | |
* Date: 5/25/2013 | |
* Time: 3:30 PM | |
* | |
*/ | |
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
namespace Utility | |
{ | |
/// <summary> | |
/// Extension methods to make IO easier | |
/// </summary> | |
public static class IOExtensions | |
{ | |
/// <summary> | |
/// Read an int (assumes the entire line contains a single integer) | |
/// </summary> | |
public static Int32 ReadInt(this StreamReader readerObj) | |
{ | |
return Convert.ToInt32(readerObj.ReadLine()); | |
} | |
/// <summary> | |
/// Read a line with an array of integers (assumes that are separated by spaces) | |
/// </summary> | |
public static List<Int32> ReadIntList(this StreamReader readerObj) | |
{ | |
List<Int32> intList = new List<Int32>(); | |
String[] inputNums = (readerObj.ReadLine()).Split(' '); | |
foreach (string singleNum in inputNums) | |
{ | |
intList.Add(Convert.ToInt32(singleNum)); | |
} | |
return intList; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Created by SharpDevelop. | |
* Date: 5/25/2013 | |
* Time: 3:06 PM | |
* | |
* Problem description: https://code.google.com/codejam/contest/351101/dashboard | |
*/ | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using Utility; | |
namespace StoreCredit | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine("Please enter input file name:"); | |
String inputFileName = Console.ReadLine(); | |
Console.WriteLine("Please enter output file name:"); | |
String outputFileName = Console.ReadLine(); | |
StreamReader fileReader = new StreamReader(inputFileName); | |
StreamWriter fileWriter = new StreamWriter(outputFileName); | |
//The first line of input gives the number of cases | |
int N = fileReader.ReadInt(); | |
//Run through the cases one by one | |
int credit; | |
int numItems; | |
List<int> items; | |
for(int i = 0; i < N; i++) | |
{ | |
credit = fileReader.ReadInt(); | |
numItems = fileReader.ReadInt(); | |
items = fileReader.ReadIntList(); | |
fileWriter.WriteLine("Case #{0}: {1}", i+1, FindItems(items, credit)); | |
} | |
fileReader.Close(); | |
fileWriter.Close(); | |
Console.ReadKey(true); | |
} | |
/// <summary> | |
/// Returns a string "A B" where A and B are the two indices whose | |
/// price adds up the store credit | |
/// </summary> | |
/// <param name="items"></param> | |
/// <param name="credit"></param> | |
/// <returns></returns> | |
private static string FindItems(List<Int32> items, int credit) | |
{ | |
//Create a boolean array of size 'credit' since we can safely ignore | |
//items that cost more than the credit we have | |
int[] itemCosts = new int[credit]; | |
int currentItem; | |
for (int i = 0; i < items.Count; i++) | |
{ | |
currentItem = items[i]; | |
if(currentItem < credit) | |
{ | |
//Find the amount required in order to fill credit with current item | |
//and another item | |
if(itemCosts[credit - currentItem] != 0) | |
{ | |
return String.Format("{0} {1}", itemCosts[credit - currentItem], i+1); | |
} | |
else | |
{ | |
itemCosts[currentItem] = i + 1; | |
} | |
} | |
} | |
return ""; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment