FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/ActivityZero/src/ActivityZero.java at master · wodgithub/algorithm · GitHub
wodgithub
algorithm
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
ActivityZero
/
src
/
ActivityZero.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
81 lines (63 loc) · 2.29 KB
Breadcrumbs
algorithm
/
ActivityZero
/
src
/
ActivityZero.java
Copy path
File metadata and controls
81 lines (63 loc) · 2.29 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
80
81
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
import
java
.
util
.
Scanner
;
/**
*
* You'll be given a list of activities and their caloric impact.
* Write a program that outputs the names of activities one should choose to partake in
* so that the sum of their caloric impact is zero.
* Once an activity is selected, it cannot be chosen again.
*
* It is a question to find a sum to zero from all possible elements of an array
* Basically use gray code to pick element from a given array and calculate the sum
*
*/
public
class
ActivityZero
{
private
String
activity
;
private
int
calorie
;
public
ActivityZero
(
String
a
,
int
c
) {
this
.
activity
=
a
;
this
.
calorie
=
c
;
}
public
static
void
main
(
String
args
[]) {
List
<
ActivityZero
>
listActivityZero
=
new
ArrayList
<
ActivityZero
>();
Scanner
scanner
=
new
Scanner
(
System
.
in
);
//TODO: input exception is not handled.
int
n
=
Integer
.
parseInt
(
scanner
.
nextLine
());
while
(
n
>
0
) {
String
newInput
=
scanner
.
nextLine
();
String
[]
tempArray
=
newInput
.
split
(
" "
);
ActivityZero
az
=
new
ActivityZero
(
tempArray
[
0
],
Integer
.
parseInt
(
tempArray
[
1
]));
listActivityZero
.
add
(
az
);
n
--;
}
scanner
.
close
();
List
<
ActivityZero
>
tmpList
=
new
ArrayList
<
ActivityZero
>();
sumCalorie
(
listActivityZero
,
listActivityZero
.
size
(),
tmpList
,
true
);
}
public
static
void
sumCalorie
(
List
<
ActivityZero
>
list
,
int
n
,
List
<
ActivityZero
>
tmpList
,
boolean
pick
) {
if
(
n
==
0
)
return
;
// start recursion back
sumCalorie
(
list
,
n
-
1
,
tmpList
,
true
);
if
(
pick
) {
tmpList
.
add
(
list
.
get
(
n
-
1
));
showSum
(
tmpList
);
}
else
{
tmpList
.
remove
(
list
.
get
(
n
-
1
));
showSum
(
tmpList
);
}
sumCalorie
(
list
,
n
-
1
,
tmpList
,
false
);
}
public
static
void
showSum
(
List
<
ActivityZero
>
l
) {
int
sumCalorie
=
0
;
for
(
ActivityZero
az
:
l
) {
sumCalorie
+=
az
.
calorie
;
}
if
(
l
.
size
() !=
0
&&
sumCalorie
==
0
) {
System
.
out
.
println
(
"The winner picks: "
);
for
(
ActivityZero
az
:
l
) {
System
.
out
.
println
(
az
.
activity
+
" "
+
az
.
calorie
);
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL