Created
December 10, 2019 05:45
-
-
Save AbsolutelySaurabh/7d87d5eef8985dc02880373dc9a3fad5 to your computer and use it in GitHub Desktop.
Max average marks of students
This file contains hidden or 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
package practice; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import java.util.Scanner; | |
public class Solution2 { | |
private static Map<String, Double> studentsMap; | |
private static double max = -1; | |
private static double getAvg(int a, int b, int c){ | |
double avg = (a+b+c)/3; | |
if(avg > max){ | |
max = avg; | |
} | |
return avg; | |
} | |
private static String maxAvg(){ | |
String output = ""; | |
for(Map.Entry<String, Double> student : studentsMap.entrySet()){ | |
if(student.getValue() == max){ | |
if(output.length() == 0){ | |
output+=student.getKey(); | |
}else | |
output = output + " " + student.getKey(); | |
} | |
} | |
output = output + " " + max; | |
return output; | |
} | |
public static void main(String[] args) { | |
Scanner s = new Scanner(System.in); | |
int test = s.nextInt(); | |
s.nextLine(); | |
while(test > 0){ | |
studentsMap = new LinkedHashMap<String, Double>(); | |
int students = 3; | |
while(students > 0){ | |
String name = s.nextLine(); | |
int marks1 = s.nextInt(); | |
int marks2 = s.nextInt(); | |
int marks3 = s.nextInt(); | |
s.nextLine(); | |
studentsMap.put(name, getAvg(marks1, marks2, marks3)); | |
students--; | |
} | |
System.out.println(maxAvg()); | |
test--; | |
} | |
s.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment