2

Each line is the same number and in all three triangles the number is the same.

Input:

  • The first line contains an integer n that represents the number of data sets to follow.
  • Each data set will consist of 1 integer m that represents the number that the Triforce of Courage contains throughout.

Output:

Print out a triforce, using this template (replacing 0 with the integer specified):

     0
    000
   00000
  0     0
 000   000
00000 00000

Assumptions: The number to be replaced will be:0<=m<=9

Sample Input: 1 2

Sample Output:

     2
    222
   22222
  2     2
 222   222
22222 22222

The following is my code thus far:

import java.lang.Math;
import java.util.Scanner;
import java.io.*;
import java.util.*;
import java.io.FileNotFoundException;

public class TriforceOfCourage {
     public static void main(String[] args) throws FileNotFoundException {
        Scanner scan=new Scanner(new File("num.dat"));
        int n = scan.nextInt();
        int count = 0;
        while(count<n) {
            for (int i=0; i<3; i++) {
                for (int k=0; k<3-i; k++) {
                    System.out.print(" ");
                }
                for (int j=0; j<i*2+1; j++) {
                    System.out.print(n);
                }
                System.out.println("");
            }
            break;
        }
    }
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Rexhep
  • 51
  • 1
  • 4
  • 3
    Ok, there's the assignment and there's your code. What's your specific programming problem? – Luiggi Mendoza Nov 11 '14 at 14:57
  • I need it to show up two more times like if was a triforce because what i have is just a triangle – Rexhep Nov 11 '14 at 15:08
  • 3
    One way to think about it is that the whole thing is one big triangle, with a hole in the middle. So, you just need to print spaces where the hole goes, and make the triangle 6 lines tall. – forgivenson Nov 11 '14 at 15:32
  • 4
    I propose a [tag:zelda] tag. – Jason C Nov 11 '14 at 15:32
  • Another approach is to separate the top and bottom half. The top is a single triangle, centered. The bottom is two triangles on either side. Splitting your logic to output the top half first, then the bottom half as a separate step could help break down the problem and simplify (each is 3 rows). Note that the bottom half can then be further divided into left and right if you choose. The modulo operator may help with the bottom half, since it can also be looked at as a repeating pattern of identical triangles. – Jason C Nov 11 '14 at 15:40
  • Yet another approach: use a template as mentioned in the task (a String containing the triangles with zeros) and then e.g. call `String result = template.replace('0','3')`. But that _may_ be not intended by the creator of the task. – Markus Ratzer Nov 11 '14 at 15:52

3 Answers3

7

There are many possible solutions to this problem. In general, there are many good strategies you would typically use to solve this:

  1. Look for a pattern. Can you find a pattern that can be easily expressed through logic and algebraic operations?
  2. Can the problem be broken down into smaller, more manageable parts? Sometimes what appears to be a complex problem can be separated into smaller, very simple problems.
  3. When in doubt, work things out on paper.

Let's take your example. At a glance, personally, I would divide the output into two halves; the top, consisting of a single triangle, and the bottom, consisting of a two triangles side by side. Further, let's focus on just being able to draw a triangle at all. Let's say we want to produce this, a triangle at an arbitrary location:

            11
  012345678901
 0       x
 1      xxx
 2     xxxxx

We notice the triangle is centered on column 7, and we suspect it will help us if we can draw a triangle in any column, so let's let center be the center column of our triangle. A lot of these types of strategies involve coming up with a way to parameterize the problem. For this approach we want to find the answer to this question:

  • Given row, column, and center, should we draw a character at that location?

Let's take a simple algebraic approach first, one row at a time, and see if we notice any patterns:

  • row == 0: Here we output only when column == center.
  • row == 1: Here we output when column >= center - 1 && column <= center + 1.
  • row == 2: Here we output when column >= center - 2 && column <= center + 2.

Notice a pattern? Think about it for a second. Recognizing that row == 0 isn't actually a special case, the pattern is:

  • Output when column >= center - row && column <= center + row.

Great! Now we can output a triangle very easily:

int center = 7; // From our example.

for (int row = 0; row < 3; ++ row) {
    for (int column = 0; column < 11; ++ column) {
        if (column >= center - row && column <= center + row)
            System.out.print("x"); // Replace with whatever character to print.
        else
            System.out.print(" ");
    }
    System.out.println(); // Line break after each row, of course.
}

But what about two triangles, for the bottom half? The simplest of course would be to do the exact same as above, but since we have two triangles, we have two centers (say, centerL and centerR), and can simply add a second if block in our bottom-half loop -- same logic for both centers, all in one loop. I'll leave this as an exercise to you.

Now, like I said, there are many possible solutions. Choose the one that makes the most sense for you and is easiest for you to get your head around. In fact, as a learning exercise, I would suggest trying to implement this program with at least three different algorithms. For example:

  • Loop over all 6 rows (instead of top and bottom half) and put all 3 triangles in the same loop.
  • Create a 2D array and draw the triangles into it, then output the contents.
  • Try to implement the above without using if at all (e.g. loops to output from center - row to center + row, and separate loops for the borders) - this is similar to your current approach.
  • Try to create a method that can draw triangles of any height, not just 3.
  • Try to create a method that can draw any "triangle of triangles", e.g. 3, 4, 5 rows of triangles.

And of course,

enter image description here

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • 2
    Interesting way to explain the solutions to the problem. And the image is awesome :D – Luiggi Mendoza Nov 11 '14 at 17:06
  • 1
    Thank you very much Mr. Jason!! You're very much appreciated and i get the way you stated it. I will post up my completed code to show you that you're hard work isn't to no avail! Again, thank you ! – Rexhep Nov 12 '14 at 14:39
2

As promised, i will be posting my solution. Thanks to MR. Jason i was able to solve this problem and now i need to make some slight modifications to where it reads in the numbers from a file but that i can comprehend with easily.

 // By Rexhep Rexhepi
 // 11/12/14
 // BIG THANKS TO MR. Jason!
 import java.lang.Math;
 import java.util.Scanner;
 import java.io.*;
 import java.util.*;
 import java.io.FileNotFoundException;

 public class TriforceOfCourage
 {

     public static void main(String[] args) throws FileNotFoundException
         {

            Scanner scan=new Scanner(new File("num.dat"));
            int n = scan.nextInt();
            int count = 0;
            int center = 5;
            int centerL = 2;
            int centerR = 8;

             for (int row = 0; row < 3; ++ row)
                 {
        for (int column = 0; column < 11; ++ column)
                {
            if (column >= center - row && column <= center + row)
                System.out.print("x"); // Replace with whatever character to print.
            else
                System.out.print(" ");
                }
        System.out.println(); // Line break after each row, of course.
            }

        for(int rowB = 0; rowB < 3; rowB++)
        {
        for (int columnL= 0;columnL < 5; columnL++)
        {
            if (columnL>= centerL - rowB && columnL <= centerL + rowB)
                System.out.print("x");
                else
                    System.out.print(" ");
        }
        for (int columnR = 5; columnR<11; columnR++)
        {
            if (columnR>= centerR-rowB && columnR <= centerR+rowB)
                System.out.print("x");
                else
                    System.out.print(" ");
        }
        System.out.println();
        }

} }

Rexhep
  • 51
  • 1
  • 4
-3

In the below code triforce(...) will print the required output.

public void triforce(int n, int m) {
    triangleWithSpaces(2, 1, n, m);
    triangleWithSpaces(2, 2, n, m);
}

public void triangleWithSpaces(int s, int t, int n, int m) {
    for (int i = 0; i <= n; i++) {
        for (int k = 0; k < t; k++) {
            for (int j = 0; j < ((n * 2 + 1) / 2 + 1) * (s - t); j++) {
                System.out.print(" ");
            }
            for (int j = n - i; j > 0; j--) {
                System.out.print(" ");
            }
            for (int j = 0; j < i * 2 + 1; j++) {
                System.out.print(m);
            }
            for (int j = n - i; j > 0; j--) {
                System.out.print(" ");
            }
            System.out.print(" ");
        }
        System.out.println();
    }
}

Sample run :

triforce(2, 0);

     0  
    000 
   00000
  0     0  
 000   000 
00000 00000

triforce(3, 9);

       9    
      999   
     99999  
    9999999 
   9       9    
  999     999   
 99999   99999  
9999999 9999999 
afzalex
  • 8,598
  • 2
  • 34
  • 61
  • 3
    This answer would be more useful to all if you could explain how you came up with what you did and what thought processes can be used to arrive at this conclusion. Doing the OP's homework for them does not help anybody. – Jason C Nov 11 '14 at 16:20
  • 1
    Also you missed an opportunity to start a code-only post with "It's dangerous to go alone, take this:". – Jason C Nov 11 '14 at 16:20
  • Difficult for me to explain it, so I made it a wiki post. In breif : in `triangleWithSpaces(s, t, n, m)`, `s` is for number of maximum triangles and `t` is number of triangles to print. – afzalex Nov 11 '14 at 16:35