티스토리 뷰

public static void main(String... args) {
List<Integer> inputs = new ArrayList<Integer>(Arrays.asList(10, 30, 10, 20, 20, 10));
int max = 100;
for (Integer input : inputs) {
max -= input;
}
if (max != 0) {
System.out.println("invalid params");
return;
}
inputs.add(0, 0);
int rand = new Random().nextInt(100) + 1;
int result = 0;
int start = 0;
int end = 0;
for (int i = 0; i < inputs.size() - 1; i++) {
start += inputs.get(i);
end += inputs.get(i + 1);
if (rand >= start && rand <= end) {
result = i;
break;
}
}
System.out.println(rand + "::dd::" + result);
}


댓글
댓글쓰기 폼