Recursive Exercise C #

4

I'm stuck with a recursive exercise in C #, then I leave the slogan:

-Make a tree as detailed below from the entry of a value of a variable.

Ex: Entry 5

 - - - - 1

 - - - 2 2 2

 - - 3 3 3 3 3

 - 4 4 4 4 4 4 4

 5 5 5 5 5 5 5 5 5

(I do not know if it looks good, but the idea is to be in the form of triangular pine)

If someone could give me a hand to solve it, I would appreciate a lot, I think there is something in the statement that I am not able to reason.

The code I could reach is the following:

class Program
{
    public class Arbol
    {
        void hacerArbol(int x)
        {
            if (x > 0)
            {
                hacerArbol(x - 1);

                if (x < 5)
                {
                    Console.Write("_" + " ");
                }
                else
                {
                    Console.Write(x);
                    Console.WriteLine();
                }
                //hacerArbol(x - 1);
            }
        }

        static void Main(string[] args)
        {
            Arbol nuevo = new Arbol();
            nuevo.hacerArbol(5);
            Console.ReadKey();
        }
    }
}

I can not manage to pose the figure that is asked, I think there is a part that I can not imagine how to do.

    
asked by user3615 27.06.2016 в 16:12
source

2 answers

3

I do not know if the order of the figure matters, I mean, whether the pine should be centered or aligned to the left or something like that.

But this code has worked for me to solve a pine tree aligned to the left:)

Its operation is simple.

public static void DrawTree(int max, int color = 0)
{
    int n = color;

    for (int i = 0; i < n; i++)
        Console.Write(n.ToString() + " ");
    Console.WriteLine();

    if (color == max) return;

    DrawTree(max, n + 1);
}

First two parameters, the first ( max ), the number of iterations that will form the pine and the last ( color ) which is a default parameter which is 0 (on the first attempt) , with these two, it is drawn along recursively calling.

public static void Main()
{
    DrawTree(5);
}

With the above code, the following output occurs:

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5

EDIT: Added information.

If you need the tree to be drawn towards the center: Add the following line before the loop for :

 Console.Write(new string(' ', max - color));

Leaving the code like this:

public static void DrawTree(int max, int color = 0)
{
    int n = color;
    Console.Write(new string(' ', max - color));
    for (int i = 0; i < n; i++)
        Console.Write(n.ToString() + " ");
    Console.WriteLine();

    if (color == max) return;

    DrawTree(max, n + 1);
}

Which produces the following output:

    1 
   2 2 
  3 3 3 
 4 4 4 4 
5 5 5 5 5 
    
answered by 27.06.2016 / 16:56
source
3

This exercise of recursion seems to me quite bad ... it has the peculiarity of needing to know the height in advance to be able to write the scripts of each floor so the function

hacerArbol(int x)

does not work; but it can be done differently:

string hacerRama(int x, int y)
{
    // Al llegar a x = 1, finaliza la recursividad.
    if (x == 1)
        return string.Concat(Enumerable.Repeat("- ", y - 1)) + '1';

    // La llamada recursiva se hace en hacerRama no en hacerArbol
    return hacerRama(x - 1, y) + Environtment.NewLine + Environtment.NewLine +
           // Cada coleccion de guiones decrece a medida que bajamos ramas
           string.Concat(Enumerable.Repeat("- ", y - x)) +
           // El tamaño de cada rama es una secuencia de numeros impares: 1, 3, 5, 7, 9...
           string.Concat(Enumerable.Repeat(x.ToString() + ' ', (x * 2) - 1));
}

string hacerArbol(int x)
{
    // hacerRama es recursiva
    return hacerRama(x, x);
}

The resulting string to call hacerArbol(5) would be exactly the one you put in the example.

The trick is to concatenate the strings in reverse by putting the first call to hacerRama at the end of the string.

PS: From two-digit numbers does not work.

    
answered by 27.06.2016 в 17:48
Solve error has leaked IntentReceiver on Android ___ ___ erkimt How hidden an entire column in a table with javascript / jquery? ______ qstntxt ___

I want to show / hide a column of a table using javascript / jquery .

Pressing a button shows or hides the entire column. The name of the column is on TH identified by an id I have achieved this by adding a class to each TD but I do not like this solution, I would like to achieve it without adding so much class attribute.

%pre% %pre%
    
______ azszpr135702 ___

Without so much code can be fixed by putting 1 button, instead of 2.

Unless it's strictly necessary, I think it's better if you only have one button, that you make %code% that commutes the visibility of the column.

To do this, we select the header with %code% and hide it or show it with %code% .

To hide / show the rows, we can make a small filter like this:

%pre%

which means: Take the %code% that are your father's third child. The parent is the row %code% and the third child is the %code% in the Country column.

and to them you apply the %code% as well.

With this you save putting the %code% to each element you want to hide.

%pre% %pre%
    
______ azszpr135706 ___

To hide with %code% , without Jquery, it occurs to me that since you already have a class that identifies you, you can use them to add a class %code% , for example.

%pre% %pre% %pre%

If you also take into account the modification of %code% , you could delete the classes of each %code% and only add a %code% to the table as well as remove a button and have only %code% that works as a %code% , but all only with only %code%

%pre% %pre% %pre%
    
______ azszpr135708 ___

You can also do the following from CSS:

%pre%

Or have a class hide in CSS and by means of javascript, add the class:

%pre%     
___