-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game of numbers.txt
79 lines (54 loc) · 1.85 KB
/
Game of numbers.txt
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.io.*;
import java.util.*;
import java.math.*;
public class Solution{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int num_games=sc.nextInt();
for(int i=0;i<num_games;i++){
int l=sc.nextInt();
int r=sc.nextInt();
int k=sc.nextInt();
ArrayList<Integer> nums=new ArrayList<Integer>();
for(int j=l;j<=r;j++){
nums.add(j);
}
int sum=0;
int count=0;
while(sum < k){
sum=sum+findOptimalMove(l,r,k,sum,nums);
count++;
}
if((count%2) != 0){
System.out.println("Alice");
}else{
System.out.println("Bob");
}
}
}
public static int findOptimalMove(int l,int r,int k,int sum,ArrayList<Integer> nums){
int diff=k-sum;
int ret=0;
if(nums.contains(diff)){
return diff;
}else{
for(int num:nums){
int temp=k-(sum+num);
if(temp == 0){
return num;
}else if( !(nums.contains(temp))){
return num;
}else{
continue;
}
}
Collections.sort(nums);
int min=nums.get(0);
int max=nums.get(nums.size()-1);
if(ret == 0){
ret=max;
}
return ret;
}
}
}