回答編集履歴

4

パターンを保存しないコードを追加

2019/12/07 05:09

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -59,3 +59,75 @@
59
59
  ```
60
60
 
61
61
  全パターンは map.get(n) で得る List<String> に入っていますので, 確認する場合はそれを表示してください.
62
+
63
+
64
+
65
+ ---
66
+
67
+ パターンを保存しない例を追加しておきます.
68
+
69
+ ```java
70
+
71
+ public class Main {
72
+
73
+ public static void main(String[] args) {
74
+
75
+ Map<Integer,Integer> map = createAllPatternCountMap(6, 3); //6面体サイコロ3個
76
+
77
+
78
+
79
+ Scanner sc = new Scanner(System.in);
80
+
81
+ int n = sc.nextInt();//目の和を入力
82
+
83
+ System.out.println("全"+map.getOrDefault(n, 0)+"件");
84
+
85
+ }
86
+
87
+
88
+
89
+ // (diceSides) 面体サイコロ (numberOfDice) 個による合計毎のパターン件数を全て抽出する.
90
+
91
+ // ループでの(6面体サイコロ2個の場合の)表現例:
92
+
93
+ // 6面体2個の全パターン数=6の2乗=36
94
+
95
+ // 全パターン <-> 6進数 <-> 10進数(i)
96
+
97
+ // 11,12,13,14,15,16 00,01,02,03,04,05 0, 1, 2, 3, 4, 5
98
+
99
+ // 21,22,23,24,25,26 10,11,12,13,14,15 6, 7, 8, 9,10,11
100
+
101
+ // 31,32,33,34,35,36 20,21,22,23,24,25 12,13,14,15,16,17
102
+
103
+ // 41,42,43,44,45,46 30,31,32,33,34,35 18,19,20,21,22,23
104
+
105
+ // 51,52,53,54,55,56 40,41,42,42,44,45 24,25,26,27,28,29
106
+
107
+ // 61,62,63,64,65,66 50.51.52.53.54.55 30,31,32,33,34,35
108
+
109
+ private static Map<Integer,Integer> createAllPatternCountMap(int diceSides, int numberOfDice) {
110
+
111
+ Map<Integer,Integer> map = new HashMap<>();
112
+
113
+ for(int i=0; i<Math.pow(diceSides,numberOfDice); i++) {
114
+
115
+ int sum = 0;
116
+
117
+ for(int j=0; j<numberOfDice; j++) {
118
+
119
+ sum += (int)(i/Math.pow(diceSides,j))%diceSides+1;
120
+
121
+ }
122
+
123
+ map.put(sum, map.getOrDefault(sum, 0)+1);
124
+
125
+ }
126
+
127
+ return map;
128
+
129
+ }
130
+
131
+ }
132
+
133
+ ```

3

10面体以上時のパターン表示でサイコロ毎の区切りがつくように区切りを挿入

2019/12/07 05:09

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -38,6 +38,8 @@
38
38
 
39
39
  sum += dice;
40
40
 
41
+ if(sb.length() > 0) sb.append('-'); //表示時用の区切り
42
+
41
43
  sb.append(dice);
42
44
 
43
45
  }

2

メソッド化

2019/12/06 17:01

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -4,25 +4,37 @@
4
4
 
5
5
  public class Main {
6
6
 
7
- static final int DICE_SIDES = 6; //6面体
8
-
9
- static final int NUMER_OF_DICE = 3; //さいころの数
10
-
11
7
  public static void main(String[] args) {
12
8
 
9
+ Map<Integer,List<String>> map = createAllPatternMap(6, 3); //6面体サイコロ3個
10
+
11
+
12
+
13
+ Scanner sc = new Scanner(System.in);
14
+
13
- //全パターン抽出
15
+ int n = sc.nextInt();//目の和を入力
16
+
17
+ System.out.println("全"+(map.containsKey(n) ? map.get(n).size() : 0)+"件");
18
+
19
+ }
20
+
21
+
22
+
23
+ // (diceSides) 面体サイコロ (numberOfDice) 個による合計毎のパターンを全て抽出する.
24
+
25
+ private static Map<Integer,List<String>> createAllPatternMap(int diceSides, int numberOfDice) {
14
26
 
15
27
  Map<Integer,List<String>> map = new HashMap<>();
16
28
 
17
- for(int i=0; i<Math.pow(DICE_SIDES,NUMER_OF_DICE); i++) {
29
+ for(int i=0; i<Math.pow(diceSides,numberOfDice); i++) {
18
30
 
19
31
  int sum = 0;
20
32
 
21
- StringBuilder sb = new StringBuilder(NUMER_OF_DICE);
33
+ StringBuilder sb = new StringBuilder(numberOfDice);
22
34
 
23
- for(int j=0; j<NUMER_OF_DICE; j++) {
35
+ for(int j=0; j<numberOfDice; j++) {
24
36
 
25
- int dice = (int)(i/Math.pow(DICE_SIDES,j))%DICE_SIDES+1;
37
+ int dice = (int)(i/Math.pow(diceSides,j))%diceSides+1;
26
38
 
27
39
  sum += dice;
28
40
 
@@ -36,13 +48,7 @@
36
48
 
37
49
  }
38
50
 
39
-
40
-
41
- Scanner sc = new Scanner(System.in);
51
+ return map;
42
-
43
- int n = sc.nextInt();//目の和を入力
44
-
45
- System.out.println("全"+(map.containsKey(n) ? map.get(n).size() : 0)+"件");
46
52
 
47
53
  }
48
54
 

1

サイコロの数を戻し忘れ

2019/12/06 16:56

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  static final int DICE_SIDES = 6; //6面体
8
8
 
9
- static final int NUMER_OF_DICE = 4; //さいころの数
9
+ static final int NUMER_OF_DICE = 3; //さいころの数
10
10
 
11
11
  public static void main(String[] args) {
12
12