Created
June 5, 2021 10:09
-
-
Save nalidzhik/a8aee3c564d9593077d543fa1ea58708 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class belowAndAboveAverage { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
String[] input = sc.nextLine().split(","); | |
int[] a = new int[input.length]; | |
for (int i = 0; i < a.length; i++) { | |
a[i] = Integer.parseInt(input[i]); | |
} | |
int size = a.length; | |
double sum = 0; | |
for (int i = 0; i < a.length; i++) { | |
sum += a[i]; | |
} | |
double avg = sum / size; | |
String[] numAsString = new String[a.length]; | |
System.out.printf("avg: %.2f", avg); | |
System.out.println(); | |
System.out.print("below: "); | |
int countBelow = 0; | |
for (int i = 0; i < size; i++) { | |
if (a[i] < avg) { | |
countBelow += 1; | |
} | |
} | |
int[] belowNumbers = new int[countBelow]; | |
int idx = 0; | |
for (int i = 0; i < a.length; i++) { | |
if (a[i] < avg) { | |
belowNumbers[idx] = a[i]; | |
idx += 1; | |
} | |
} | |
String[] numbersBelowAsString = new String[belowNumbers.length]; | |
for (int i = 0; i < belowNumbers.length; i++) { | |
numbersBelowAsString[i] = Integer.toString(belowNumbers[i]); | |
} | |
System.out.println(String.join(",", numbersBelowAsString)); | |
System.out.print("above: "); | |
int countAbove = 0; | |
for (int i = 0; i < size; i++) { | |
if (a[i] > avg) { | |
countAbove += 1; | |
} | |
} | |
int[] aboveNumbers = new int[countAbove]; | |
int index = 0; | |
for (int i = 0; i < a.length; i++) { | |
if (a[i] > avg) { | |
aboveNumbers[index] = a[i]; | |
index += 1; | |
} | |
} | |
String[] numbersAsString = new String[aboveNumbers.length]; | |
for (int i = 0; i < aboveNumbers.length; i++) { | |
numbersAsString[i] = Integer.toString(aboveNumbers[i]); | |
} | |
System.out.println(String.join(",", numbersAsString)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment