javaで重複している組み合わせを省きたいです。
lang
1import java.io. *; 2 3public class Main { 4 public static void main(String[] args) throws Exception { 5 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 6 int dice = Integer.parseInt(br.readLine()); 7 int[] s = new int[3]; 8 int a = 0; 9 int count = 0; 10 11 while(a < 6){ 12 s[a] = Integer.parseInt(br.readLine()); 13 a += 1; 14 } 15 16 for(int i = 0; i < 3; i++){ 17 for(int j = 0; j < 3; j++){ 18 for(int k = 0; k < 3; k++){ 19 int sum = s[i] + s[j] + s[k]; 20 if(sum == dice ){ 21 count += 1; 22 } 23 } 24 } 25 } 26 System.out.println(count); 27 } 28}
6つのサイコロを投げます。その中から三つの目を選んで足して、指定した数値(int dice)になるのは何通りあるか調べたいです。
上のようなソースを書いたのですが、重複も含まれてしまうので重複の省き方を知りたいです。
回答3件
あなたの回答
tips
プレビュー