c# - Code jam Store Credit -
problem receive credit c @ local store , buy 2 items. first walk through store , create list l of available items. list buy 2 items add entire value of credit. solution provide consist of 2 integers indicating positions of items in list (smaller number first).
input first line of input gives number of cases, n. n test cases follow. each test case there be: • 1 line containing value c, amount of credit have @ store. • 1 line containing value i, number of items in store. • 1 line containing space separated list of integers. each integer p indicates price of item in store. • each test case have 1 solution.
output each test case, output 1 line containing "case #x: " followed indices of 2 items price adds store credit. lower index should output first. limits 5 ≤ c ≤ 1000 1 ≤ p ≤ 1000 small dataset n = 10 3 ≤ ≤ 100 large dataset n = 50 3 ≤ ≤ 2000
sample data input
3 100 3 5 75 25 200 7 150 24 79 50 88 345 3 8 8 2 1 9 4 4 56 90 3
output case #1: 2 3 case #2: 1 4 case #3: 4 5
using system; using system.collections.generic; using system.linq; using system.text; using system.io; namespace readcreaditcard { class program { static void main(string[] args) { //ask input file path. console.writeline("please enter input file path:"); //@"c:\users\nirbhay\document\store credit-small.txt"; string path = console.readline(); //validate file path. while (!file.exists(path)) { console.writeline("please enter valid file path:"); path = console.readline(); } //open file read data. using (streamreader tr = new streamreader(path)) { //read no of cases. string noofcases = tr.readline(); int casecounter = 0; //validate if first line of input file valid numeric value. while (!int.tryparse(noofcases, out casecounter)) { console.writeline("first line of input file not numeric value."); console.writeline("please enter numeric value:"); noofcases = console.readline(); } int caseno = 1; //read lines of data , show in reverse order. while (casecounter-- > 0) { #region read credit value string creditstr = tr.readline(); if (creditstr == null) { console.writeline("invalid input file."); break; } int creditvalue = 0; if (!int.tryparse(creditstr, out creditvalue)) { console.writeline("invalid input file."); break; } #endregion #region read items count string itemscountstr = tr.readline(); if (itemscountstr == null) { console.writeline("invalid input file."); break; } int itemscount = 0; if (!int.tryparse(itemscountstr, out itemscount)) { console.writeline("invalid input file."); break; } #endregion string itemspricestr = tr.readline(); if (itemspricestr == null) { console.writeline("invalid input file."); break; } string[] arrayofcosts = itemspricestr.split(' '); printitems(caseno, creditvalue, itemscount, arrayofcosts); caseno++; } } console.writeline("\n \n press key exit :)"); console.readkey(); } /// <summary> /// prints indexes of selected items. /// </summary> /// <param name="caseno"></param> /// <param name="creditvalue"></param> /// <param name="itemscount"></param> /// <param name="arrayofcosts"></param> private static void printitems(int caseno, int creditvalue, int itemscount, string[] arrayofcosts) { (int = 0; < itemscount; i++) { int firstitemcost = int.parse(arrayofcosts[i]); (int j = + 1; j < itemscount; j++) { int seconditemcost = int.parse(arrayofcosts[j]); if (firstitemcost + seconditemcost == creditvalue) { console.writeline("case#{0}: {1}, {2}", caseno, i, j); return; } } } } } }
Comments
Post a Comment