For example: [3, 3, 2, 2, 2, 4, 3, 5, 4, 6, 3]=>2,2,2,4;4,6 result is 2
[3, 3, 2,3, 2, 2, 4, 3, 5, 4, 6, 3]=>2;2,2,4;4,6 result is 3
Implementation ideas:
Convert array remainder to 01 array [3, 3, 2, 2, 2, 4, 3, 5, 4, 6, 3]=>[1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1]
Counting the number of consecutive 0 is the number of even series
int[] arr = {3,3,2,2,2,4,3,5,4,6,3 };int[] Arrnew =New int[arr. Length]; for(inti =0; I < arr. Length; i++) {Console.Write (arr[i]); intresult = Arr[i]%2; Arrnew[i]=result;} Console.WriteLine ();intOddcount =0, Evencount =0;intCurrent =0, next =0; for(inti =0; I < arrnew.length-1; i++) { current=Arrnew[i]; Console.Write (current); Next= Arrnew[i +1]; if(Current = =0&& Next! =Current ) Evencount++; if(Current = =1&& Next! =Current ) Oddcount++; if(i = = arrnew.length-2) { if(Next = =0) evencount++; Elseoddcount++; }}console.writeline (Arrnew[arrnew.length-1]); Console.WriteLine ("odd: {0}, even: {1}", Oddcount, Evencount); Console.readkey ();
c#-the number of consecutive even sequences in an int array