مساله:
بازی Greed بازی است که با پنج تاس ۶ وجهی بازی می شود. ماموریت شما، که اگر آنرا قبول کنید، نحوه امتیاز گرفتن با این قوانین است. همیشه به شما یک آرایه با پنج مقدار تاس ۶ وجهی داده می شود. قوانین :
Three 1's => 1000 points Three 6's => 600 points Three 5's => 500 points Three 4's => 400 points Three 3's => 300 points Three 2's => 200 points One 1 => 100 points One 5 => 50 point
Description:
Greed is a dice game played with five six-sided dice. Your mission, should you choose to accept it, is to score a throw according to these rules. You will always be given an array with five six-sided dice values.
Three 1's => 1000 points Three 6's => 600 points Three 5's => 500 points Three 4's => 400 points Three 3's => 300 points Three 2's => 200 points One 1 => 100 points One 5 => 50 point
A single die can only be counted once in each roll. For example, a given “5” can only count as part of a triplet (contributing to the 500 points) or as a single 50 points, but not both in the same roll.
Example scoring
Throw Score --------- ------------------ 5 1 3 4 1 250: 50 (for the 5) + 2 * 100 (for the 1s) 1 1 1 3 1 1100: 1000 (for three 1s) + 100 (for the other 1) 2 4 4 5 4 450: 400 (for three 4s) + 50 (for the 5)
In some languages, it is possible to mutate the input to the function. This is something that you should never do. If you mutate the input, you will not be able to pass all the tests.
import java.util.HashMap; import java.util.Map; public class Greed { public static int greedy(int[] dice) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 1; i <= 6; i++) map.put(i, 0); for (int d : dice) { int val = map.get(d); map.put(d, ++val); } int result = 0; result += (map.get(1) / 3) * 1000; result += (map.get(6) / 3) * 600; result += (map.get(5) / 3) * 500; result += (map.get(4) / 3) * 400; result += (map.get(3) / 3) * 300; result += (map.get(2) / 3) * 200; result += (map.get(1) % 3) * 100; result += (map.get(5) % 3) * 50; return result; } }
public class Greed { public static int greedy(int[] dice) { int n[] = new int[7]; for (int d : dice) n[d]++; return n[1]/3*1000 + n[1]%3*100 + n[2]/3*200 + n[3]/3*300 + n[4]/3*400 + n[5]/3*500 + n[5]%3*50 + n[6]/3*600; } }
public class Greed{ public static int greedy(int[] dice){ int res = 0; int[] count = new int[]{0, 0, 0, 0, 0, 0}; int[] weight = new int[]{100, 0, 0, 0, 50, 0}; int[] weight3 = new int[]{1000, 200, 300, 400, 500, 600}; for (int die : dice) count[die-1]++; for (int i = 0; i < count.length; i++) res+=(count[i]/3*weight3[i]) + (count[i]%3 * weight[i]); return res; } }
import java.util.HashMap; public class Greed{ public static int greedy(int[] dice){ int sum = 0; HashMap<Integer, Integer> c = new HashMap<Integer, Integer>(); for(int i = 0; i < dice.length; i++) { if(c.containsKey(dice[i]) && c.get(dice[i]) != null) { c.put(dice[i], c.get(dice[i])+1); } else { c.put(dice[i], 1); } } for(int i = 1; i < 7; i++) { if(c.get(i) != null) { int tmp = c.get(i); sum += score(i, (tmp-tmp%3)/3, tmp%3); } } return sum; } public static int score (int n, int three, int one) { int result = 0; switch (n) { case 1: result = three * 1000 + one * 100; break; case 2: result = three *200; break; case 3: result = three *300; break; case 4: result = three *400; break; case 5: result = three *500 + one *50; break; case 6: result = three *600; break; default: break; } return result; } }
import java.util.Arrays; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; public class Greed{ public static int greedy(int[] dice){ Map<Integer, Long> collect = Arrays.stream(dice).boxed().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); int score = 0; for (Map.Entry<Integer, Long> e : collect.entrySet()) { switch (e.getKey()) { case 1: score += ( ( e.getValue() >= 3) ? 1000 : 0) + (e.getValue() % 3) * 100; break; case 2: score += ( ( e.getValue() >= 3) ? 200 : 0); break; case 3: score += ( ( e.getValue() >= 3) ? 300 : 0); break; case 4: score += ( ( e.getValue() >= 3) ? 400 : 0); break; case 5: score += ( ( e.getValue() >= 3) ? 500 : 0) + (e.getValue() % 3) * 50; break; case 6: score += ( ( e.getValue() >= 3) ? 600 : 0); break; } } return score; } }