Софтуерно Инженерство
Loading...
krasizorbov avatar krasizorbov 142 Точки

06. Equal Sums Left Right Position. Programming basics excercises

Здравейте,

Някой би ли ми помогнал? Програмата работи и отговорите са верни но ми дава memory limit и поради това имам 80 от 100.

Може би заради преобразуването. Явно има по интелигентен начин.

Ето и кода:

using System;

namespace Equal_Sum
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = int.Parse(Console.ReadLine());
            int num2 = int.Parse(Console.ReadLine());

            int sumLeft = 0;

            int sumRight = 0;

            int middle = 0;

            for (int i = num1; i <= num2; i++)
            {
                for (int j = 0; j < i.ToString().Length; j++)
                {
                    char first = i.ToString()[j];

                    string sub = first.ToString();

                    int num3 = 0;

                    bool number_digit = Int32.TryParse(sub, out num3);

                    if (number_digit)
                    {
                        if (j == 0 || j == 1)
                        {
                            sumLeft = sumLeft + num3;
                        }
                        else if (j == 2)
                        {
                            middle = num3;
                        }
                        else if (j == 3 || j == 4)
                        {
                            sumRight = sumRight + num3;
                        }
                    }
                }
                if (sumLeft < sumRight)
                {
                    sumLeft = sumLeft + middle;

                    if (sumLeft == sumRight)
                    {
                        Console.Write("{0} ",i);
                    }
                }
                else if (sumLeft > sumRight)
                {
                    sumRight = sumRight + middle;

                    if (sumLeft == sumRight)
                    {
                        Console.Write("{0} ", i);
                    }
                }
                else if (sumLeft == sumRight)
                {
                    Console.Write("{0} ", i);
                }

                sumLeft = 0;
                sumRight = 0;
                middle = 0;

            }
        }
    }
}

 

Тагове:
0
C# Fundamentals
DimitarTsvetkov avatar DimitarTsvetkov 1 Точки

I think you are missing "=" in the second for loop (int j = 0; j < i.ToString().Length; j++). At the moment is "<" and it has to be "<=". Sorry for not responding in Bulgarian. I don't and I can't have Cyrilic on this computer. I hope that helps :)

0