Zadanie na rekurencje

1

Cześć, mam zadanie do rozwiązania o treści "Stwórz funkcję string Linia3(uint n), która działa rekurencyjnie i zwraca tekst (string) zawierający jedynie parzyste liczby z przedziału od n do 1 (włącznie), oddzielone przecinkiem. Po ostatniej liczbie brak przecinka. Przykład zwróconego tekstu: "8,6,4,2""

Napisałam następujący kod ale zwraca liczby od 0 a w przypadku liczby nieparzystej zwraca o jedną za dużo i nie bardzo wiem co zrobić aby działało poprawnie:

static string Linia3(uint n)
{
if (n>0)
{
if (n % 2 == 0)
{
return string.Format("{0},{1}",n, Linia3(n - 2));
}
return string.Format("{0},{1}",n-1, Linia3(n - 1));
}
else
{
return "0";
}
}
static void Main(string[] args)
{
uint a = 5, b = 8;
Console.WriteLine(Linia3(a));
Console.WriteLine(Linia3(b));

        Console.ReadKey();
    }
1

Na pewno return "0"; w tym nie pomaga.

Zamiast: return string.Format("{0},{1}",n-1, Linia3(n - 1));
Zrób: return Linia3(n - 1);

W linijce: return string.Format("{0},{1}",n, Linia3(n - 2)); dodaj .TrimEnd(',')

2

Coś takiego? : )

using System;

namespace Rekrutacyjne
{
    class Program
    {
        static void Main()
        {
            uint a;

            Console.WriteLine("Podaj liczbę:");
            a = UInt32.Parse(Console.ReadLine());
            
            Console.WriteLine(Linia3(a));

            Console.ReadKey();
        }
        static string text = null;
        static string Linia3(uint n)
        {
            if (n != 0)
            {
                Linia3(n - 1);
            }
            if (n == 2)
            {
                text = n + text;
            }
            else if(n % 2 == 0 && n != 0)
            {
                text = n + "," + text;
            }
            return text;
        }
    }
}

1
static string Linia3(uint n)
{
if (n>0)
{
if (n % 2 == 0)
{
return string.Format("{0},{1}",n, Linia3(n - 2));
}
return string.Format("{0},{1}",n-1, Linia3(n - 1));
}
else
{
return "0";
}
}
static void Main(string[] args)
{
uint a = 5, b = 8;
Console.WriteLine(Linia3(a));
Console.WriteLine(Linia3(b));

        Console.ReadKey();
    }
1
static string Linia3(uint n)
{
if (n>0)
{
if (n % 2 == 0)
{
return string.Format("{0},{1}",n, Linia3(n - 2).TrimEnd(','));
}
return return Linia3(n - 1);
}
else
{
return "";
}
}
static void Main(string[] args)
{
uint a = 5, b = 8;
Console.WriteLine(Linia3(a));
Console.WriteLine(Linia3(b));

        Console.ReadKey();
    }

dla 2 i3 będzie działało:

static string Linia3(uint n)
{
if (n>0)
{
if(n==2) return "2";
if (n % 2 == 0)
{
return string.Format("{0},{1}",n, Linia3(n - 2).TrimEnd(','));
}
return return Linia3(n - 1).TrimEnd(',');
}
else
{
return "";
}
}
static void Main(string[] args)
{
uint a = 5, b = 8;
Console.WriteLine(Linia3(a));
Console.WriteLine(Linia3(b));

        Console.ReadKey();
    }
2

Twój kod poprawiony i dla "3" też działa

using System;

namespace std
{
    class rekurencja
    {
        static string Linia3(uint n)
        {
            if (n > 0)
            {
                if (n % 2 == 0)
                {
                    return string.Format("{0},{1}", n, Linia3(n - 2));
                }
                else
                {
                    return string.Format("{0}", Linia3(n - 1));
                }
            }
            else
            {
                return "0";
            }
        }
        static void Main(string[] args)
        {
            uint a = 5, b = 8;
            Console.WriteLine(Linia3(a));
            Console.WriteLine(Linia3(b));
            Console.WriteLine(Linia3(3));

            Console.ReadKey();
        }
    }
}
```

1 użytkowników online, w tym zalogowanych: 0, gości: 1