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

Това е моето решение.

using System;

namespace EqualPairs
{
    class Program
    {
        static void Main(string[] args)
        {

            int number = int.Parse(Console.ReadLine());
            int evenSum = 0;
            int oddSum = 0;
            int diff = 0;
            bool areTheSumsEqual = true;

            for (int position=1; position<=number; position++)
            {
                int currentNumberOne = int.Parse(Console.ReadLine());
                int currentNumberTwo = int.Parse(Console.ReadLine());

                if(position%2==0)
                {
                    evenSum = currentNumberOne + currentNumberTwo;
                }
                else
                {
                    oddSum = currentNumberTwo + currentNumberOne;
                }

                if(position>1 && Math.Abs(evenSum-oddSum)>diff)
                {
                    diff = Math.Abs(evenSum - oddSum);
                     areTheSumsEqual = false;
                }
            }

            if (areTheSumsEqual == true)
            {
                Console.WriteLine($"Yes, value={oddSum}");
            }
            else
            {
                Console.WriteLine($"No, maxdiff={diff}");
            }
        }
    }
}

0
Maver avatar Maver 5 Точки

Мерси!

0
badsector avatar badsector 1 Точки

Това ми се струва, че нямаше да мине в Judge, ако имаше вход, при който най-голямата разлика не е при последните суми.

Ето още един вариант на C# :

using System;

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

            double sum = 0;
            double difference = 0;
            double maxDifference = 0;

            for (int i = 0; i < n; i++)
            {
                double num1 = double.Parse(Console.ReadLine());
                double num2 = double.Parse(Console.ReadLine());
                double currentSum = num1 + num2;
                if (i == 0)
                {
                    sum = currentSum;
                }
                else
                {
                    difference = Math.Abs(sum - currentSum);
                    sum = currentSum;
                }                
                if (difference > maxDifference)
                {
                    maxDifference = difference;
                }                
            }
            if (maxDifference == 0)
            {
                Console.WriteLine($"Yes, value={sum}");
            }
            else
            {
                Console.WriteLine($"No, maxdiff={maxDifference}");
            }            
        }
    }
}

0
14/02/2019 23:00:29
nikeza avatar nikeza 2 Точки

Ето още едно решение:

import java.util.Scanner;

public class loop_FOR_exercises {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        

        int n = Integer.parseInt(scanner.nextLine());

        int firstSum = 0;
        int midSum = 0;
      

        int diff1 = 0;

        boolean check = true;

        for (int i = 1; i <= n; i++) {
            int numA = Integer.parseInt(scanner.nextLine());
            int numB = Integer.parseInt(scanner.nextLine());

            if (i % 2 != 0) {
                firstSum = numA + numB;
            } else {
                midSum = numA + numB;
            }

            if (firstSum == midSum || n < 2) {
                check = true;
            } else {
                diff1 = Math.abs(midSum - firstSum);
                check = false;

            }

        }

        if (check) {
            System.out.printf("Yes, value=%d", firstSum);
        } else {

            System.out.printf("No, maxdiff=%d", diff1);
        }


    }
}




0
badsector avatar badsector 1 Точки

C#

using System;

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

            double sum = 0;
            double difference = 0;
            double maxDifference = 0;

            for (int i = 0; i < n; i++)
            {
                double num1 = double.Parse(Console.ReadLine());
                double num2 = double.Parse(Console.ReadLine());
                double currentSum = num1 + num2;
                if (i == 0)
                {
                    sum = currentSum;
                }
                else
                {
                    difference = Math.Abs(sum - currentSum);
                    sum = currentSum;
                }                
                if (difference > maxDifference)
                {
                    maxDifference = difference;
                }                
            }
            if (difference == 0)
            {
                Console.WriteLine($"Yes, value={sum}");
            }
            else
            {
                Console.WriteLine($"No, maxdiff={maxDifference}");
            }            
        }
    }
}

0
AlexMarinov87 avatar AlexMarinov87 16 Точки

JavaScript:

https://pastebin.com/4RjNxkEB

0