Решение на задача 04. Equal Pairs - For Loop Exercise
Здравейте,
Някой може ли да предостави решение на задача 04. Equal Pairs - For Loop Exercise, която да е дала 100 точки? Благодаря предварително!
Здравейте,
Някой може ли да предостави решение на задача 04. Equal Pairs - For Loop Exercise, която да е дала 100 точки? Благодаря предварително!
Още едно решение
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)); } } }