Софтуерно Инженерство
Loading...
+ Нов въпрос
danail2003 avatar danail2003 4 Точки

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 242 Точки

Здравей,

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

Защо ти е помощната променлива 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