c# - Dynamically Writing to a List -
so trying solve problem:
you given number n , 2*n numbers. write program check whether sum of odd numbers equal sum of n numbers. first number considered odd, next even, next odd again, etc. print result “yes” or “no”. in case of yes, print sum. in case of no, print difference between odd , sums.
input
the input data should read console.
• first line holds integer n – count of numbers.
• each of next 2*n lines holds 1 number.
the input data valid , in format described. there no need check explicitly.
output
• output must printed on console.
• print “yes, sum=s” s sum of odd n numbers in case of sum of odd n numbers equal sum of n numbers.
• otherwise print “no, diff=d” d difference between sum of odd n numbers , sum of n numbers. d should positive number. constraints
• number n integer in range [0...500].
• other numbers integers in range [-500 000 ... 500 000].
• allowed working time program: 0.25 seconds.
• allowed memory: 16 mb."
and current code, compiles this:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace odd_and_even_sum { class program { static void main(string[] args) { int n = int.parse(console.readline()); list<string> stringofnumbers = new list<string>(2*n); stringofnumbers.defaultifempty(); list<int> listofevennumbers = new list<int>(2*n); list<int> listofoddnumbers = new list<int>(2*n); int diff = 0; (int = 0; < 2*n; i++) { stringofnumbers[i] = console.readline(); } list<int> listofnumber = new list<int>() { 1, 2, 3 }; (int = 0; < 2*n; i++) { listofnumber[i] = int.parse(stringofnumbers[i]); } foreach (var item in listofnumber) { if (iseven(item)) { listofevennumbers.add(item); } else { listofoddnumbers.add(item); } } int sumofeven = listofevennumbers.sum(); int sumofodd = listofoddnumbers.sum(); if (sumofeven > sumofodd){ diff = sumofeven - sumofodd; } if (sumofeven < sumofodd){ diff = sumofodd - sumofeven; } if (sumofeven == sumofodd) { console.writeline("yes, sum ={0}", sumofeven); } else { console.writeline("no, diff={0}", diff); } } public static bool iseven(int n) { if (n % 2 == 0) { return true; } else { return false; } } } }
my code compiling, when insert input values errror:
unhandled exception: system.argumentoutofrangeexception: index out of range. must non-negative , less size of collection. parameter name: index @ system.throwhelper.throwargumentoutofrangeexception() @ system.collections.generic.list`1.set_item(int32 index, t value) @ odd_and_even_sum.program.main(string[] args) in c:\users\user1\documents\v isual studio 2013\projects\odd , sum\program.cs:line 21
when write
list<string> stringofnumbers = new list<string>(2*n);
you creating new empty list have zero element have initial capacity bound of 2*n. means it reserves memory inserting 2*n elements not have any.
stringofnumbers[i] = console.readline();
here trying access elements of list cause exception because there no element in list. should instead use
stringofnumbers.add(console.readline());
Comments
Post a Comment