java求0—7所能组成的奇数个数

1个回答

  • public class CountTest {

    /**

    * @param args

    */

    public static void main(String[] args) {

    // TODO Auto-generated method stub

    int i = 7;

    System.out.println("0—" + i + "所能组成的奇数个数:");

    count(i);

    }

    public static int count(int num) {

    if(num == 0)

    return 0;

    if(num == 1)

    return 1;

    int sum = 0;

    int temp = 0;

    if(num%2 == 0)

    temp = num/2;

    else

    temp = num/2 + 1;

    sum += temp;

    System.out.println("1位数" + sum + "个,算法:偶数除以2;奇数除以2加1");

    for(int i=0; i