Programming Fundamentals Arrays -Number Array Mid Exam - 30 June 2019 Group 1
Здравейте, колеги:)
От известно време се боря с масиви и конкретно с тази задача. Проблемът ми е , че решението ми работи(
using System;
using System.Linq;
public class Program
{
public static void Main()
{
long[] numbers=Console.ReadLine().Split().Select(long.Parse).ToArray();
string input=null;
while((input=Console.ReadLine())!="End")
{
string[] command=input.Split().ToArray();
long index=0;
long sumNegative=0;
long sumPositive=0;
long sumAll=0;
switch (command[0])
{
case"Switch":
index=long.Parse(command[1]);
if(index<0||index>=numbers.Length)
{
continue;
}
numbers[index]=numbers[index]*-1L;
break;
case"Change":
index=long.Parse(command[1]);
if(index<0||index>=numbers.Length)
{
continue;
}
long numValue=long.Parse(command[2]);
numbers[index]=numValue;
break;
case"Sum":
string commandSum=command[1];
if(commandSum=="Negative")
{
for(int i=0;i<numbers.Length;i++)
{
if(numbers[i]<0)
{
sumNegative+=numbers[i];
}
}
Console.WriteLine(sumNegative);
}
else if(commandSum=="Positive")
{
for(int i=0;i<numbers.Length;i++)
{
if(numbers[i]>=0)
{
sumPositive+=numbers[i];
}
}
Console.WriteLine(sumPositive);
}
else if(commandSum=="All")
{
for(int i=0;i<numbers.Length;i++)
{
sumAll+=numbers[i];
}
Console.WriteLine(sumAll);
}
break;
}
}
foreach(int number in numbers)
{
if(number>=0)
{
Console.Write(number+" ");
}
}
}
}
), но Джъдж не го приема и ми дава следната грешка:
C:\Windows\TEMP\ExecutionStrategies\mqwjvphd.wn1\tmp6B78.tmp:79 run: using System; ^^^^^^ SyntaxError: Unexpected identifier at Module._compile (internal/modules/cjs/loader.js:872:18) at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10) at Module.load (internal/modules/cjs/loader.js:790:32) at Function.Module._load (internal/modules/cjs/loader.js:703:12) at Function.Module.runMain (internal/modules/cjs/loader.js:999:10) at internal/main/run_main_module.js:17:11
Моля ви за помощ, защото наистина не мога да разбера какъв е проблемът!
Предварително благодаря!
Задачата е:
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.
Поздрави.
Дени
Благодаря много! Мога да си отдъхна:)