-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_fit1.java
More file actions
42 lines (34 loc) · 1.25 KB
/
first_fit1.java
File metadata and controls
42 lines (34 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.*;
public class first_fit1 {
public static void no_of_bins(int item_weights[], int cap, int n) {
int res = 0;
int bin_rem[] = new int[n];
for (int i = 0; i < n; i++) {
int j;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= item_weights[i]) {
bin_rem[j] = bin_rem[j] - item_weights[i];
break;
}
}
if (j == res) {
bin_rem[res] = cap - item_weights[i];
res++;
}
}
System.out.println("Number of bins required in First Fit " + res);
}
public static void main (String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no. of items u want to add in a bin");
int n = sc.nextInt();
System.out.println("Enter the bin capacity");
int cap = sc.nextInt();
int items_weight[] = new int[n];
System.out.println("Enter the items");
for (int i = 0; i < n; i++) {
items_weight[i] = sc.nextInt();
}
no_of_bins(items_weight, cap, n);
}
}