Art
I tried doing some quick renderings with Lightwave last night but things just weren't panning out. I wanted to do something creative but want I really wanted to do was get better at Command & Conquer Generals. So I figure, why not have the best of both worlds? What is art, after all? Certainly more than oil and canvas or pretty pixels lined up in a row. So here are some examples of alternative art: me whipping the computer on "Brutal" and another programming contest answer written in Haskell.
QUESTION 5 - BILLS AND COINS
The newly established republic of Northopia is in the process of defining its monetary system, and is looking for a logical way of deciding on the number of denomination of the units of its money (that is, it's bills and coins). The government's assumption is that the total number of bills and coins that must be exchanged during a cash transaction is a measure of the "goodness" of the number of denomination selection.
Government consultants have already decided that the smallest unit of money will be worth exactly one "Northing," and that all other units of money must be worth an integral number of Northings. They have also agreed that there should be no more than ten different monetary units, none worth more than 10,000 Northings.
You have been employed to write a program to help in determining the goodness of a selection of monetary units. Given a description of a set of proposed units of money available in Northopia, and the total amount of a transaction, your program is to determine the fewest number of bills and coins that can be used in concluding the transaction.
For example, suppose five units of money are proposed, and that they are worth 1, 5, 50, 100 and 500 Northings. A 999 Northing transaction could be completed by paying one 500-Northing unit, four 100-Northing units one 50-Northing unit, nine 5-Northing units and four 1-Northing units, and then receiving no units as change, for a total of 19 units of money exchanged in the transaction. The minimum number of units however, would be obtained by paying with two 500-Northing units and then receiving one 1-Northing unit in change, for a total of 3 units of money involved in the transaction.
INPUT:
A file with a single line named "dataFile5.txt" containing the following data: an integer N that indicates the proposed number of units of money. This integer is then followed by N integers representing the proposed value of each of those N units of money (in one Northing units), one of which will be 1 Northing, as defined by the consultants. Finally, the amount of a proposed transaction is given as an integer, again in one Northing units. All transaction amounts will be non-negative.
OUTPUT:
For each input case, display the minimum number of units of money required to complete the transaction.
EXAMPLE
Sample Input: Expected Output:
5 1 5 50 100 500 999 Case 1: 3 units required
10 1 2 4 8 16 32 64 128 256 512 999 Case 2: 5 units required
Haskell
-- Main program loop
main = do
-- Hugs might not be able to find the file unless the path is specified
file <- readFile "input.txt"
showLines (lines file) 1
return ()
-- Do the calculation for each line
showLines :: [String] -> Integer -> IO ()
showLines [] y = return ()
showLines x y = do
let line = processLine (head x)
putStr ("Case " ++ show(y) ++ ": " ++ line ++ " units required\n")
showLines (tail x) (y+1)
return ()
-- Do the calculation for a given line
processLine x = show ( getMinUnits (reverse values) sum 0 )
where
tokens = getTokens x
sum = head (reverse tokens)
values = init (drop 1 tokens)
-- List of currency units, Remainder, current count, return TotalCount
getMinUnits [] y z = z -- Any empty list returns 0
getMinUnits x y z = min
(getMinUnits (tail x) (highAmount y numTimes currentVal) z+numTimes)
(getMinUnits (tail x) (lowAmount y numTimes currentVal) z+numTimes+1)
where
currentVal = head x
numTimes = coinsForSum y ( currentVal )
-- Utility functions
coinsForSum x y = abs x `div` y
highAmount x y z = (abs x) - y*z
lowAmount x y z = (abs x) - (y+1)*z
-- Split the string into Integer tokens
getTokens [] = []
getTokens x = (read firstElement::Integer) : getTokens (concatMap (++ " ") tailElements)
where
tokens = words x
firstElement = head tokens
tailElements = tail tokens
Here is the sample input.
|
C#
using System;
using System.IO;
namespace Question5 {
class Class1 {
static int getMinUnits(int[] types, int offset, int amount, int totalCount) {
if (offset == -1) return totalCount;
int thisDenom = types[offset];
int numTimes = Math.Abs(amount) / thisDenom;
//Console.WriteLine("Testing amount: " + amount);
//Console.WriteLine(thisDenom + " numTimes: " + numTimes);
int newAmount1 = Math.Abs(amount) - numTimes*thisDenom;
int newAmount2 = Math.Abs(amount) - (numTimes+1)*thisDenom;
//Console.WriteLine(" High: " + newAmount1);
//Console.WriteLine(" Low: " + newAmount2);
int high = getMinUnits(types, offset-1, newAmount1, totalCount+numTimes);
int low = getMinUnits(types, offset-1, newAmount2, totalCount+numTimes+1);
return Math.Min(high, low);
}
static int getInt(string str) {
return Int32.Parse(str);
}
static void Main(string[] args) {
if (!File.Exists("dataFile5.txt")) {
Console.WriteLine("Input file not found");
return;
}
StreamReader sr = File.OpenText("dataFile5.txt");
string input = "";
int index = 0;
while ((input = sr.ReadLine()) != null) {
if (input == "") return;
string[] data = input.Split(' ');
int numUnits = getInt(data[0]);
if (data.Length > numUnits + 2
/* replacing this with: numUnits != data.Length-2 would have won me the contest */
|| numUnits > 10) {
Console.WriteLine("Case "
+ index++ + ": Incorrect number of units: "
+ numUnits);
return;
}
int[] unitVals = new int[data.Length-2];
bool foundOne = false;
for (int i=1; i < data.Length-1; i++) {
unitVals[i-1] = getInt(data[i]);
if (unitVals[i-1] > 10000) {
Console.WriteLine("Case " + index++
+ ": Unit too large");
continue;
}
if (unitVals[i-1] == 1) {
foundOne = true;
}
}
if (!foundOne) {
Console.WriteLine("Case " + index++
+ ": Unit of value 1 not found");
continue;
}
int sum = 0;
try {
sum = getInt(data[data.Length-1]);
} catch(FormatException fe) {
Console.WriteLine("Case " + index++
+ ": Incorrect number of units or missing sum");
}
Console.WriteLine("Case "
+ index++ + ": "
+ getMinUnits(unitVals, unitVals.Length-1, sum, 0)
+ " units required");
}
}
}
}
|
|