Console Input/Output - Task 14 - Sum of Elements
Здравейте,
Имам въпрос по 14-та задача от домашното за Console Input/Output - както и да напиша кода, тест 11 и тест 12 не ми се получават и имам 83/100 точки. Проверих и авторското решение, с изключение на лууп-а, всичко ми е почти еднакво. Ето моя код:
using System;
class SumOfElements
{
static void Main()
{
string NumberLine = Console.ReadLine();
string[] Numbers = NumberLine.Split(' ');
int BiggestNumber = 0;
int Sum = 0;
foreach (string value in Numbers)
{
int SpecialNumber = int.Parse(value);
if (SpecialNumber > BiggestNumber)
{
BiggestNumber = SpecialNumber;
}
Sum = Sum + SpecialNumber;
}
if (Sum == 2*BiggestNumber)
{
Console.WriteLine("Yes, sum={0}",BiggestNumber);
}
else
{
Console.WriteLine("No, diff={0}",Math.Abs(Sum - 2*BiggestNumber));
}
}
}
Условието на задачата е:
Sum of Elements
This problem is from Variant 3 of C# Basics exam from 11-04-2014 Morning. You can test your solution here .
You are given n numbers. Find an element that is equal to the sum of all of the other elements.
Input
Input data should be read from the console.
• At the only line in the input a sequence of integers stays (numbers separated one from another by a space).
The input data will always be valid and in the format described. There is no need to check it explicitly.
Output
The output data must be printed on the console. At the only line of the output print the result.
• Print "Yes, sum=…" if there is an element that is equal to the sum of all other elements, along with this sum.
• Print "No, diff=…" if there is no element that is equal to the sum of all other elements. Print also the minimum possible difference between an element from the sequence and the sum of all other elements (always a positive number).
Constraints
• All input numbers are integers in the range [0 … 2 000 000 000].
• The count n of the input integers is in the range [2 ... 1 000].
• Allowed working time for your program: 0.1 seconds.
• Allowed memory: 16 MB.