-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.java
More file actions
61 lines (53 loc) · 1.07 KB
/
Permutations.java
File metadata and controls
61 lines (53 loc) · 1.07 KB
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
* A solution to the Permutations problem.
*
* @author Isidore Sossa
*/
public class Permutations
{
/**
* Main method.
*
* @param args No parameter required.
*/
public static void main(String[] args)
{
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
solution(in.nextLong());
in.close();
}
/**
* Print a beautiful permutation of integers 1,2,…,n. If there are several
* solutions, you may print any of them. If there are no solutions,
* print "NO SOLUTION".
*
* @param n Length of permutation.
*/
private static void solution(long n)
{
if (n >= 5)
{
long nextNumber = n;
for (long i = 0; i < n; i++)
{
System.out.printf("%d%s", nextNumber, (i == n - 1) ? "" : " ");
nextNumber -= 2;
if (nextNumber < 0)
{
nextNumber += n;
}
else if (nextNumber == 0)
{
nextNumber += (n - 1);
}
}
}
else
{
System.out.println("NO SOLUTION");
}
}
}