implement MainExercises.java #1
@@ -146,11 +146,57 @@ public class MainExercises
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public int[][] intPartitions(int n) {
|
public int[][] intPartitions(int n) {
|
||||||
// todo
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
ArrayList<int[]> result = new ArrayList<>();
|
||||||
|
|
||||||
|
int[] currentPartition = new int[n];
|
||||||
|
|
||||||
|
int k=0; //اخرین ایندکس currentPartition
|
||||||
|
|
||||||
|
currentPartition[k] = n;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
int[] temp = new int[k+1];
|
||||||
|
for(int i=0 ; i<=k ; i++) {
|
||||||
|
temp[i] = currentPartition[i];
|
||||||
|
}
|
||||||
|
result.add(temp);
|
||||||
|
|
||||||
|
int remained = 0;
|
||||||
|
while(k>=0 && currentPartition[k]==1) {
|
||||||
|
remained += currentPartition[k];
|
||||||
|
k--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(k<0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
currentPartition[k]--;
|
||||||
|
remained++;
|
||||||
|
|
||||||
|
|
||||||
|
while (remained > currentPartition[k]) {
|
||||||
|
currentPartition[k+1] = currentPartition[k];
|
||||||
|
remained -= currentPartition[k];
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentPartition[k+1] = remained;
|
||||||
|
k++;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int[][] output = new int[result.size()][];
|
||||||
|
for(int i=0 ; i<result.size() ; i++) {
|
||||||
|
output[i] = result.get(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public static void main()
|
public static void main()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user