blocks[worstIndex]) { "> blocks[worstIndex]) { "> blocks[worstIndex]) { ">
import java.util.*;
public class WorstFit {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of jobs:");
int jobn = sc.nextInt();
System.out.println("Enter jobs:");
int job[] = new int[jobn];
for (int i = 0; i < jobn; i++) {
job[i] = sc.nextInt();
}
System.out.println("Enter no. of blocks:");
int block = sc.nextInt();
System.out.println("Enter blocks:");
int blocks[] = new int[block];
int totalBlocks = 0;
for (int i = 0; i < block; i++) {
blocks[i] = sc.nextInt();
totalBlocks += blocks[i];
}
int sumAllocatedJobs = 0;
for (int i = 0; i < jobn; i++) {
int worstIndex = -1;
for (int j = 0; j < block; j++) {
if (job[i] <= blocks[j]) {
if (worstIndex == -1 || blocks[j] > blocks[worstIndex]) {
worstIndex = j;
}
}
}
if (worstIndex != -1) {
sumAllocatedJobs += job[i];
System.out.println(" Job " + job[i] + " allocated to block " + (worstIndex + 1));
blocks[worstIndex] -= job[i];
} else {
System.out.println(" Job " + job[i] + " cannot be allocated.");
}
}
double avgWorst = (double) sumAllocatedJobs / totalBlocks;
System.out.printf("avg_worst:- %.2f\\n", avgWorst);
}
}
