FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ProjectEuler-Java/Sol24.java at master · JoshP/ProjectEuler-Java · GitHub
JoshP
ProjectEuler-Java
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
ProjectEuler-Java
/
Sol24.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
61 lines (56 loc) · 1.69 KB
Breadcrumbs
ProjectEuler-Java
/
Sol24.java
Copy path
File metadata and controls
61 lines (56 loc) · 1.69 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
/*
The following algorithm generates the next permutation lexicographically after a given permutation.
It changes the given permutation in-place.
Find the largest index k such that a[k] < a[k + 1]. If no such index exists,
the permutation is the last permutation.
Find the largest index l such that a[k] < a[l]. Since k + 1 is such an index,
l is well defined and satisfies k < l.
Swap a[k] with a[l].
Reverse the sequence from a[k + 1] up to and including the final element a[n].
*/
import
java
.
util
.*;
import
java
.
math
.*;
public
class
Sol24
extends
BaseSolution
{
public
void
runSolution
() {
int
[]
initial
=
new
int
[]{
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
};
int
count
=
1
;
while
(
count
!=
1000000
) {
count
++;
next
(
initial
);
}
printArray
(
initial
);
}
public
void
next
(
int
[]
a
) {
int
k
= -
1
;
for
(
int
i
=
a
.
length
-
2
;
i
>=
0
;
i
--) {
if
(
a
[
i
] <
a
[
i
+
1
]) {
k
=
i
;
break
;
}
}
if
(
k
== -
1
) {
System
.
out
.
println
(
"found last"
);
return
;
}
int
l
=
k
+
1
;
for
(
int
i
=
l
;
i
<
a
.
length
;
i
++) {
if
(
a
[
k
] <
a
[
i
])
l
=
i
;
}
swap
(
k
,
l
,
a
);
for
(
int
i
=
k
+
1
,
end
= ((
a
.
length
-
k
+
1
) /
2
) +
k
;
i
<
end
;
i
++) {
swap
(
i
,
a
.
length
-
i
+
k
,
a
);
}
}
public
void
printArray
(
int
[]
a
) {
for
(
int
i
:
a
) {
System
.
out
.
print
(
i
);
}
System
.
out
.
println
();
}
public
void
swap
(
int
i
,
int
j
,
int
[]
a
) {
int
temp
=
a
[
i
];
a
[
i
] =
a
[
j
];
a
[
j
] =
temp
;
}
}
Back
|
FazBrowse Home
|
New Git URL