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

这是规律:
个位数1、3、5、7:是4个
十位数是7*4:28个(11、13...77)

百位数是:7*4*8:224个

7*4*8*8
每次需上面的数乘以8
只要个位数是1,3,5,7就行
请问用算法怎么解决!

0—7所能组成的奇数个数:
1位数4个,算法:偶数除以2;奇数除以2加1
2位数24个,算法:6*4
3位数144个,算法:6*6*4
4位数720个,算法:6*6*5*4
5位数2880个,算法:6*6*5*4*4
6位数8640个,算法:6*6*5*4*3*4
7位数17280个,算法:6*6*5*4*3*2*4
8位数17280个,算法:6*6*5*4*3*2*1*4
总数 46972 个
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<num; i++) {
int temp1 = temp;
int temp2 = num-1;
boolean boo = true;
StringBuffer sb = new StringBuffer();
for(int j=0; j<i+1; j++) {
sb.append(temp2).append("*");
temp1 = temp2 * temp1;
if(boo) {
boo = false;
continue;
}
temp2--;
}
sum += temp1;
System.out.println((i+2) + "位数" + temp1 + "个,算法:" + sb + temp);
}
System.out.println("总数 " + sum + " 个");
return sum;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-07-10

1 2 3 4 5 6 7

//一个简单的算法,用8进制来表示就行
public class ColorChange {
    public static void main(String[] args) {
        int count = 0;
        for(int i = 00; i < 010; i++){  //个位数字
            if(i % 2 != 0){
                System.out.printf("%o ",i);
                count ++;
            }
        }
        System.out.println("个位数共计:"  + count);
        
        count = 0;
        for(int i = 010; i < 0100; i++){  //十位数字
            if(i % 2 != 0){
                System.out.printf("%o ",i);
                count ++;
            }
        }
        System.out.println("十位数共计:"  + count);
        
        count = 0;
        for(int i = 0100 ; i < 01000; i++){  //百位数字
            if(i % 2 != 0){
                System.out.printf("%o ",i);
                count ++;
            }
        }
        System.out.println("百位数共计:"  + count);
    }
}

本回答被网友采纳
第2个回答  2014-01-09
//这个是基础训练吧。
//貌似是经典50题中的逻辑分析。首先你的逻辑不够清晰。建议用笔在纸上拆分数字。求出最大值
//和最小值,然后思路清晰后。利用代码写出来。代码如下:
public class Num{public static void main(String[] args){ int count = 0; //声明由数字组成的数 int n = 8; //一位数 count = n/2; //两位数 count += (n-1)*n/2; //三位数 count += (n-1)*n*n/2; //四位数 count += (n-1)*n*n*n/2; //五位数 count += (n-1)*n*n*n*n/2; //六位数 count += (n-1)*n*n*n*n*n/2; //七位数 count += (n-1)*n*n*n*n*n*n/2; System.out.println("0-7所能组成的奇数个数:"+count); }}
//实现步骤:逐步拆分数字。一直到7位数、就行。奇数的属性你首先得熟悉。希望采纳!
第3个回答  2018-03-27
学过排列组合吗?上面题目中看着有0,但你没说,那么如果我们忽略0,你要的情况就是7种排列组合来搞定,这个编辑器上边不好打,就是高中的那种,A7,1 A7,2 A7,3 A7,4 A7,5 A7,6 A7,7 也就是用循环的方式求这七种方式下的技术,在每一种组合中只挑出奇数即可,挑的时候可以不要去判断它的末位,可以与2相除取余,如果余数为1则是奇数
第4个回答  2017-05-28

不知道我这样做可不可以

//求0-7所能组成的6位数的奇数个数是多少个?

class Test{
    public static void main(String[] args){
        int count = 0;//记录奇数的个数
        for(int i=100000; i<=777777; i++){ //最小的6位数是100000 最大的6位数是777777
            if(i % 2 != 0){  //判断是否为奇数
                count++;
                System.out.println("第"+count+"个奇数是: "+i);
            }
        }
        System.out.println("奇数个数是: "+count);//打印奇数的个数
    }
}

相似回答