标签:else lin com lse i++ result write nbsp []
例如:[3, 3, 2, 2, 2, 4, 3, 5, 4, 6, 3]=>2,2,2,4;4,6 结果为2
[3, 3, 2,3, 2, 2, 4, 3, 5, 4, 6, 3]=>2;2,2,4;4,6 结果为3
实现思路:
将数组取余转换为01数组[3, 3, 2, 2, 2, 4, 3, 5, 4, 6, 3]=>[1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1]
算出连续为0的个数就是偶数列的个数
int[] arr = { 3, 3, 2, 2, 2, 4, 3, 5, 4, 6, 3 }; int[] arrNew = new int[arr.Length]; for (int i = 0; i < arr.Length; i++) { Console.Write(arr[i]); int result = arr[i] % 2; arrNew[i] = result; } Console.WriteLine(); int oddCount = 0, evenCount = 0; int current = 0, next = 0; for (int i = 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++; else oddCount++; } } Console.WriteLine(arrNew[arrNew.Length - 1]); Console.WriteLine("奇数:{0},偶数:{1}", oddCount, evenCount); Console.ReadKey();
标签:else lin com lse i++ result write nbsp []
原文地址:https://www.cnblogs.com/zhyue93/p/CSharp-1.html