请教数字拆分程序编写!

sunlarry 2007-11-09 12:00:00

将数字: 0, 1, 2,3,4,5,6,7,8,9 这十个数字任意5个拆分成两组, 并打印出每种拆分结果!


谢谢!!
...全文
127 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunlarry 2007-11-12
  • 打赏
  • 举报
回复
太谢谢了,解帖
seaforce 2007-11-09
  • 打赏
  • 举报
回复
252是表示:排列组合的意思,就是从10个元素中取出不5个的所有可能情况.
A52=(10*9*8*7*6)/(5*4*3*2*1)=252种
seaforce 2007-11-09
  • 打赏
  • 举报
回复

/**
* project_name: Test
* package_name: netsource
* package_declaration: package netsource;
* filename: DivNumber.java
* author: yuhaiming
* date: 2007-11-9
*/
package netsource;
import java.math.*;
public class DivNumber {

/**
* 主处理函数
*/
public static void disposal(){
int[][] answer = new int[252][5];
int[] a = new int[5];
int answersize = 0;
while(true){
int acount=0;

//生成数组序列
while(true){
//得到一个0-9的随机数
int randomvalue = (int)(Math.random()*10);
//System.out.println(randomvalue);
if(checkRandomValue(a,randomvalue)){
a[acount++]=randomvalue;
//System.out.println("检验通过");
}
if(acount>=5)break;
}

//打印该数组
printinfo(a);

//检验生成的数组,如果满足则拷贝
if(true){

System.out.println("长度:"+answersize);

//printAnswer(answer,answersize-1);
System.out.println("检验通过,开始拷贝");
for(int i=0;i<5;i++){
answer[answersize][i]=a[i];
}
answersize++;

//System.out.println("***********************************");

printAnswer(answer,answersize);
}

if(answersize>=252)break;
}
}


/**
* 打印数组
* @param inf
*/
public static void printinfo(int[] inf){
for(int i=0;i<inf.length;i++){
System.out.print(inf[i]+" ");
}
System.out.println();
}

/**
* 检查已分组是否重复
* @param curanswer
* @param checkvalue
* @return
*/
public static boolean checkAnswerArray(int[][] curanswer,int[] checkvalue,int answersize){

//对已有数组集进行遍历处理,检查出是否有相同的分组
for(int i=0;i<answersize;i++){
//重复元素统计
int count = 0;
//统计总共重复元素个数
for(int j=0;j<checkvalue.length;j++){
if(checkRandomValue(curanswer[i],checkvalue[j]))
count++;
}
//计数器为5说明,该数组和原来某条数据一致,返回失败标志
if(count==5)return false;
}

return true;
}

/**
* 检查新生成的数是否在已赋值的数组中,防止重复赋值
* @param result
* @param checkvalue
* @return
*/
public static boolean checkRandomValue(int[] result,int checkvalue){
for(int i=0;i<result.length;i++){
if(checkvalue==result[i])return false;
}
return true;
}

/**
* 打印最终结果集
*/
public static void printAnswer(int[][] answerPrint,int answersize){
System.out.println("总共有"+answersize+"种分组:");
for(int i=0;i<answersize;i++){
for(int j=0;j<5;j++){
System.out.print(answerPrint[i][j]+" ");
}
System.out.println("");
System.out.println("-------------");
}

}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
disposal();
//printAnswer();
}

}

sunlarry 2007-11-09
  • 打赏
  • 举报
回复
ding

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧