c# - "myList.Count" is less then the amount of elements inside the list -
i have task write program takes numbers , step
input.
must make sequence of binary representation of numbers , destroy bits @ positions 1
, 1*step
, 2*step
, 3*step
...
here code:
using system; using system.collections.generic; class bitkiller { static void main() { int amountnumbers = int.parse(console.readline()), step = int.parse(console.readline()), counter = 0, number = 0 ; int[] numbin= new int[8], numbers = new int[amountnumbers] ; var sequence = new list<int>(); for(int = 0; < amountnumbers; i++) { numbers[i] = int.parse(console.readline()); numbin = tobin(numbers[i]); sequence.insertrange(counter * 8, numbin); foreach(int b in sequence) { console.write(b); } console.writeline(""); counter++; } if(step == 1) { console.writeline(0); return; } for(int = sequence.count; >= 0; i--) { if(i % step == 1) { sequence.removeat(i); } } console.writeline("list count = {0}", sequence.count); if(sequence.count % 8 != 0) { int padding = 8 - (sequence.count % 8); for(int = 0; < padding; i++) { sequence.add(0); } } foreach(int b in sequence) { console.write(b); } console.writeline(""); for(int = 7, power = 0, y = 0; y < sequence.count; i--, y++, power++) { number = number + (sequence[i]) * topower(2, power); if(i == 0) { console.writeline("result = {0}", number); sequence.removerange(0, 8); foreach(int b in sequence) { console.write(b); } console.writeline(""); number = 0; = 7; y = 0; power = 0; } } } static int[] tobin(int number) { var binsequence = new int[8]; for(int = 7; >= 0; number /= 2, i--) { if(number % 2 == 0 || (number == 0 && >= 0)) { binsequence[i] = 0; } else { binsequence[i] = 1; } } return binsequence; } static int topower(int number, int power) { int numberreturn = number; if(power == 0) { return 1; } if(number == 1) { return number; } for(int = 0; < power - 1; i++) { numberreturn = numberreturn * number; } return numberreturn; } }
now, there couple of print lines can see binary numbers coming input numbers.
in short program converts numbers lists containing '1' , '0' , removes values list according formula.
main question why is:
sequence.count
returning 22, when there 24 '1's inside list.
test following input: 3,19,255,255,255
.
result be: 255, 254, 252
, while correct output 255, 255, 252
.
it happening, because of code:
if(sequence.count % 8 != 0) { int padding = 8 - (sequence.count % 8); for(int = 0; < padding; i++) { sequence.add(0); } }
because sequence.count
22 ( why? ) condition true , following loop replacing last 2 '1's 2 zeros.
causing wrong output.
point.
why sequence.count
equal 22, when there 24 '1's in list.
sorry, if bit long , confusing, posted whole code, because have no idea , how interfering cause issue.
you removing 2 items list in code:
for(int = sequence.count; >= 0; i--) { if(i % step == 1) { sequence.removeat(i); } }
with example input gave, sequence.removeat
being called when == 20 , when == 1. there 24 items, removed 2 of them.
Comments
Post a Comment