Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

danail2003 avatar danail2003 27 Точки

Number Array

Не мога да си открия грешката, ако мо же помощ. Имам 90/100.

Create a program that helps you keep track of a number array. First, you are going to receive the numbers оn a single line, separated by space, in the following format:

"{number1} {number2} {number3}… {numbern}"

Then you will start receiving commands until you read the "End" message. There are five possible commands:

  • "Switch {index}"
    • Find the number on this index in your collection, if the index exists, and switch its sign (negative <-> positive).  
  • "Change {index} {value}"
    • Replace the number on the given index with the number given, if the index exists.
  • "Sum Negative"
    • Print the sum of all negative numbers.
  • "Sum Positive"
    • Print the sum of all positive numbers.
  • "Sum All"
    • Print the sum of all numbers.

In the end, print the positive numbers on a single line, keeping in mind that 0 is positive, separated by a single space in the following format:

"{number1} {number2} {number3}… {numbern}"

Input

  • On the 1st line you are going to receive the numbers of the array (always integers), separated by a single space.
  • On the next lines, until the "End" command is received, you will be receiving commands.

Output

  • Print the tasks in the format described above.

 

 

Examples

Input

Output

1 2 3 4 5
Switch 4
Change 0 -3
Sum Negative
End

-8
2 3 4

Comments

First, we receive the command "Switch 4" and we make the number on index 4 negative (because it is positive before the command). After this command, the task collection looks like this:

1 2 3 4 -5

Afterwards, we receive the "Change 0 -3" command and we need to change the number on index 0 with the number -3. The collection looks like this now:

-3 2 3 4 -5

After that, we receive the "Sum Negative" command, which means we need to print the sum of all negative numbers and it is -8.

In the end, we print all of the positive numbers. This is the result collection:

2 3 4

 

 

1 2 3 4 5 4 3 2 1 0
Switch -4
Change 13 0
Switch 0
Sum All
End

23
2 3 4 5 4 3 2 1 0

 

 

using System;
using System.Linq;

namespace Number_Array
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = Console.ReadLine().Split().Select(int.Parse).ToArray();
            string command = Console.ReadLine();
            int sumNegative = 0;
            int sumPositive = 0;
            int sumAll = 0;

            while (command != "End")
            {
                string[] token = command.Split();

                if (token[0] == "Switch")
                {
                    int index = int.Parse(token[1]);

                    for (int i = 0; i < numbers.Length; i++)
                    {
                        if (index == i)
                        {
                            int swithNumber = numbers[i];
                            swithNumber *= -1;
                            numbers[i] = swithNumber;
                        }
                    }
                }
                else if (token[0] == "Change")
                {
                    int index = int.Parse(token[1]);
                    int value = int.Parse(token[2]);

                    for (int i = 0; i < numbers.Length; i++)
                    {
                        if (i == index)
                        {
                            numbers[i] = value;
                        }
                    }
                }
                else if (token[0] == "Sum")
                {
                    if (token[1] == "Negative")
                    {
                        for (int i = 0; i < numbers.Length; i++)
                        {
                            if (numbers[i] < 0)
                            {
                                sumNegative += numbers[i];
                            }
                        }
                    }
                    else if (token[1] == "Positive")
                    {
                        for (int i = 0; i < numbers.Length; i++)
                        {
                            if (numbers[i] > 0)
                            {
                                sumPositive += numbers[i];
                            }
                        }
                    }
                    else if (token[1] == "All")
                    {
                        for (int i = 0; i < numbers.Length; i++)
                        {
                            sumAll += numbers[i];
                        }
                    }
                }

                command = Console.ReadLine();
            }

            if (sumNegative < 0)
            {
                Console.WriteLine(sumNegative);
            }
            else if (sumPositive > 0)
            {
                Console.WriteLine(sumPositive);
            }
            else if(sumAll != 0)
            {
                Console.WriteLine(sumAll);
            }

            for (int i = 0; i < numbers.Length; i++)
            {
                if (numbers[i] >= 0)
                {
                    Console.Write(string.Join(" ", numbers[i] + " "));
                }
            }
        }
    }
}

Тагове:
0
Programming Fundamentals
krum_43 avatar krum_43 750 Точки

Здравей,

При първата проверка правиш излишни неща.

Защо ти е помощната променлива switchNumber?

numbers[i[*=-1;

Иначе в условието е казано,че нулата спада към положителните числа.

In the end, print the positive numbers on a single line, keeping in mind that 0 is positive, separated by a single space in the following format:

"{number1} {number2} {number3}… {numbern}"=>при проверката за положителни числа имаш:

  if (numbers[i] >= 0).

 

 

0
23/09/2019 07:33:24
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.