-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDice.java
More file actions
32 lines (31 loc) · 997 Bytes
/
Dice.java
File metadata and controls
32 lines (31 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.ArrayList;
public class Dice {
public static void main(String[] args) {
int t=4;
diceValue("", t);//void function call
System.out.println(diceList("", t));//arraylist function call
}
//function that returns the no of possibilities of achieveing the given target in a dice
static void diceValue(String s, int target){
if(target==0){
System.out.println(s);
return;
}
for(int i=1;i<=6 && i<=target;i++){
diceValue(s+i, target-i);
}
}
//function that returns the possibilities but in an ArrayList
static ArrayList<String> diceList(String s, int target){
if(target==0){
ArrayList<String> list=new ArrayList<>();
list.add(s);
return list;
}
ArrayList<String> ans=new ArrayList<>();
for(int i=1;i<=6 && i<=target;i++){
ans.addAll(diceList(s+i, target-i));
}
return ans;
}
}