Решение на задача 04. Equal Pairs - For Loop Exercise
Здравейте,
Някой може ли да предостави решение на задача 04. Equal Pairs - For Loop Exercise, която да е дала 100 точки? Благодаря предварително!
Здравейте,
Някой може ли да предостави решение на задача 04. Equal Pairs - For Loop Exercise, която да е дала 100 точки? Благодаря предварително!
Това е моето решение.
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}");
}
}
}
}
Ето още едно решение:
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); } } }
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}");
}
}
}
}
JavaScript:
https://pastebin.com/4RjNxkEB
Още едно решение
import java.util.*;
public class LabTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int couplesCount=Integer.parseInt(sc.nextLine());
int num1=Integer.parseInt(sc.nextLine());
int num2=Integer.parseInt(sc.nextLine());
int previousSum=num1+num2;
int maxDiff=0;
for (int i=2;i<=couplesCount; i++){
num1=Integer.parseInt(sc.nextLine());
num2=Integer.parseInt(sc.nextLine());
int currentSum=num1+num2;
if (currentSum != previousSum ){
maxDiff=Math.abs(currentSum-previousSum);
previousSum=currentSum;
}
}
if (maxDiff==0){
System.out.printf("Yes, value=%d",previousSum);
}else{
System.out.printf("No, maxdiff=%d",maxDiff);
}
}
}
Още 1 опростено решение на Java, 100 points from Judge:
package ForLoopExercisePlus; import java.util.Scanner; public class P08EqualPairs { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = Integer.parseInt(scanner.nextLine()); int firstSum = 0; int secondSum = 0; int diff = 0; for (int i = 1; i <= n; i++) { int n1 = Integer.parseInt(scanner.nextLine()); int n2 = Integer.parseInt(scanner.nextLine()); if (i % 2 != 0) { firstSum = n1 + n2; } else { secondSum = n1 + n2; } } diff = Math.abs(firstSum - secondSum); if (firstSum == secondSum || n < 2) { System.out.printf("Yes, value=%d", firstSum); } else { System.out.printf("No, maxdiff=%d", diff); } } }
Споделям и моето решение :) -------------------------------- package ForLoop.MoreExercises; import java.util.Scanner; public class EqualPairs_08 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = Integer.parseInt(scanner.nextLine()); int sum = 0; int currentSum = 0; int maxCurrentDiff = Integer.MIN_VALUE; int previousSum = 0; int diff = 0; for (int i = 1; i <= n; i++) { previousSum = currentSum; int numberOne = Integer.parseInt(scanner.nextLine()); int numberTwo = Integer.parseInt(scanner.nextLine()); currentSum = numberOne + numberTwo; if (i > 1 && i <= n) { diff = Math.abs(currentSum - previousSum); } if (diff > maxCurrentDiff) { maxCurrentDiff = diff; } } if (diff == 0) { System.out.printf("Yes, value=%d", currentSum); } else { System.out.printf("No, maxdiff=%d", Math.abs(maxCurrentDiff)); } } }
Мерси!
Това ми се струва, че нямаше да мине в 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}");
}
}
}
}