1 package test_4_2; 2 3 public class CountsOfUnevenNum { 4 5 public static void main(String[] args) { 6 7 /** 求0,1,2,3,4,5,6,7所能组成的8位奇数个数。 */ 8 9 int[] numArray = {0, 1, 2, 3, 4, 5, 6, 7}; 10 11 int unevenNum = countUneven(numArray); 12 int zero = countZero(numArray); 13 int midNum = numArray.length - zero - 1; 14 15 int countMid = getCounts(midNum); 16 int count = (numArray.length - zero) * countMid * unevenNum; 17 18 System.out.println("可组成" + count + "个奇数"); 19 20 } 21 22 /** 阶乘 */ 23 private static int getCounts(int numCount) { 24 25 int count = 1; 26 27 for (int i = 1; i <= numCount; i++) { 28 count *= i; 29 } 30 31 return count; 32 33 } 34 35 /** 0的个数 */ 36 private static int countZero(int[] numArray) { 37 38 int count = 0; 39 40 for (int i = 0; i < numArray.length; i++) { 41 if (numArray[i] == 0) { 42 count++; 43 } 44 45 } 46 47 return count; 48 } 49 50 /** 奇数个数 */ 51 private static int countUneven(int[] numArray) { 52 53 int count = 0; 54 55 for (int i = 0; i < numArray.length; i++) { 56 if (numArray[i] % 2 == 1) { 57 count++; 58 } 59 } 60 61 return count; 62 } 63 64 }
结果如下:
可组成2880个奇数