# Java Algorithm

• March 29th, 2021, 04:28 PM
salisu14
Java Algorithm
There is playground of length N=10. Given two different squares of length 1, the other of length 2, and the number of the two kinds of squares is not limited. If the children want to lay the squares out in the playground, a total of how many different ways to place the squares?

For example, the ground of length 4 has a total of 5 types of laying methods as follows

4=1+1+1+1
4=2+2
4=2+1+1
4=1+2+1
4=1+1+2
• March 29th, 2021, 05:58 PM
Norm
Re: Java Algorithm
Do you have any specific java programming questions?
• March 30th, 2021, 01:16 PM
salisu14
Re: Java Algorithm
Yes, this one is to write a java program to solve the problem.

Here is my java code but does no do the job well:

import java.util.Scanner;

public class Permutation {
int nNumber = 0;
int computeLays(int arr[], int index, int num, int reducedNum) {
if (reducedNum < 0)
return 0;
if (reducedNum == 0) {
for (int i = 0; i < index; i++) {
if(arr[i] != num) {
String temp = " " + arr[i];
System.out.print(temp);
}
}

nNumber += 1;
System.out.println();
return 0;
}

int prev = (index == 0) ? 1 : arr[index - 1];
for (int k = prev; k <= num; k++) {
arr[index] = k;
computeLays(arr, index + 1, num, reducedNum - k);
}
return nNumber-1;
}

void findComputeCombinations(int n) {
int arr[] = new int[n];
int result = computeLays(arr, 0, n, n);
System.out.println(result);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
Permutation computer = new Permutation();
computer.findComputeCombinations(n);
}
}
• March 30th, 2021, 01:19 PM
Norm
Re: Java Algorithm
Ok, once you get the algorithm, try writing the java code for it.
If you have any problems writing the code, you should post post the logic for the algorithm and your questions here. I will try to help with the java programming problems.