In: Computer Science
Code Using Arrays in Java
There is a new ruling for the only elevator located at Block B. Students who need to ride the elevator, must line up in a queue. The rated load in pounds for the elevator is based on the inside net platform areas. The maximum load for the elevator is 500 pound.
Write a program to read the students weight (in pound) in the line and calculate the number of students that are allowed to enter the elevator before it makes a loud noise.
Input
The first line of input is T (1 ≤ T ≤ 100) which is the number of test case. This is followed by T lines of input. Each line starts with X (X ≤ 100) which is the number of students who want to ride the elevator. This is then followed by a list of X data which is the students’ weight in the line.
Output
For each test case, the output contains a line in the format "Case #x: y", where x is the case number (starting from 1) and y indicates the number of students in the line that are allowed to ride the elevator.
Sample Input
3 9 45 25 50 46 10 55 50 83 68 5 66 155 93 101 90 8 64 70 50 45 85 74 110 95
Sample Output
Case #1: 9 Case #2: 4 Case #3: 7
Code below:
import java.util.Scanner;
public class Elevator {
private static final int MAX_LOAD_ALLOWED = 500;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// take number of test cases as input.
int t = scanner.nextInt();
int[] numberOfStudentsAllowed = new int[t];
for (int caseNumb = 0; caseNumb < t; ++caseNumb) {
// take inputs per test case.
int n = scanner.nextInt();
int[] studentsWeightsArray = new int[n];
// defaulting students allowed to value n for every case Numb.
numberOfStudentsAllowed[caseNumb] = n;
for (int i = 0; i < n; ++i) {
studentsWeightsArray[i] = scanner.nextInt();
}
// find the total number of students that will be allowed.
int tempSum = 0;
for (int i = 0; i < n; ++i) {
tempSum += studentsWeightsArray[i];
if (tempSum > MAX_LOAD_ALLOWED) {
numberOfStudentsAllowed[caseNumb] = i;
}
}
}
// Print the outputs.
for (int i = 0; i < t; ++i) {
System.out.println("Case #" + (i + 1) + ": " + numberOfStudentsAllowed[i]);
}
}
}
Sample input output

Sample code for indentation

