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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java Algorithm

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post 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

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Algorithm

    Do you have any specific java programming questions?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. ctt-sp algorithm(cost transitive tournament shortest path algorithm)
    By seeniya in forum Java Theory & Questions
    Replies: 3
    Last Post: April 22nd, 2014, 05:36 AM
  2. Replies: 16
    Last Post: November 21st, 2013, 08:49 AM
  3. Replies: 3
    Last Post: June 10th, 2013, 04:42 AM
  4. Simplex algorithm java
    By musicalwatch in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 29th, 2012, 02:14 PM
  5. Schoof's algorithm in Java
    By octavianaugustin in forum Java Theory & Questions
    Replies: 2
    Last Post: June 15th, 2011, 02:26 PM

Tags for this Thread