package cn.yangg.scala.base.init
class LotteryChoiceAlgor(list:List[Int],selectCount:Int) {
val totalLayer=selectCount
val length=list.length
vartotalChoice=0
def mainAlgor():Unit={
valstartIndex=(-1)
valcurrentLayer=1
val item=""
this.excute(startIndex,currentLayer,item)
}
/**
* 1层:startIndex=0,endIndex=length-(totalLayer-1)-1
* 2层:startIndex=itemIndex+1,endIndex=length-(totalLayer-2)-1
* n层:startIndex=itemIndex+1,endIndex=length-(totalLayer-n)-1
*/
private def excute(itemIndex:Int,currentLayer:Int,str:String):Unit={
valstartIndex=itemIndex+1//第N层计算可选排列元素的开始索引
val endIndex=length-(totalLayer-currentLayer)-1//第N层计算可选排列元素的结束索引
if(currentLayer<totalLayer){
for(index <-startIndex toendIndex){
excute(index,currentLayer+1,str+list(index)+"->")
}
}else if(currentLayer==totalLayer){
for(index <-startIndex toendIndex){
println(str+list(index))
this.totalChoice+=1
}
}
}
def printTotalChoice={
println("total choice Count:"+this.totalChoice)
}
}
object LotteryStart{
def main(args:Array[String]){
val choiceAlgor=new LotteryChoiceAlgor((1 to 34).toList,6)
choiceAlgor.mainAlgor
choiceAlgor.printTotalChoice
}
}