Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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  Reply With Quote

2. ## Re: Java Algorithm

Do you have any specific java programming questions?  Reply With Quote

3. ## 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);
}
}  Reply With Quote

4. ## 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.  Reply With Quote 