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

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

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;
}
}

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 个
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-03-13
public static void main(String[] args) {
//7个数字最多组成的是7位数,所以要小于10000000
int size = 10000000;
int count = 0;
//循环所有的数
for(int i = 0; i < size; i++) {
//偶数排除掉
if(i % 2 == 0) {
continue;
}
//将数字转换成字符串
String str = String.valueOf(i);
//在这个字符串中,包含8和9的都不是想要的
if(str.contains("8") || str.contains("9")) {
continue;
}
//如果该数字符合要求,就将计数器加1
count++;
//输出了符合要求的数,由于数量太大,你可以分段打印出来想要的结果
//System.out.print(i);
}
System.out.println(count);
}
第2个回答  2009-05-06
排列了算啊..几位数.确定否.
相似回答