Code:
import java.io.*;
class Bestfit
{
int fn,bn;
int file[],block[],bflag[],fragment[];
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
Bestfit() throws IOException
{
int i;
System.out.println("Enter the number of
files");
fn=Integer.parseInt(input.readLine());
System.out.println("Enter the number of
blocks");
bn=Integer.parseInt(input.readLine());
file=new int[fn+1];
block=new int[bn+1];
bflag=new int[bn+1];
fragment=new int[fn+1];
for(i=0;i<bn;i++)
bflag[i]=0;
}
void accept() throws IOException
{
int i;
System.out.println("Enter the size of each
file");
for(i=0;i<fn;i++)
file[i]=Integer.parseInt(input.readLine());
System.out.println("Enter the size of each
block");
for(i=0;i<bn;i++)
block[i]=Integer.parseInt(input.readLine());
}
void execute()
{
int i,j,min,p=-1;
for(i=0;i<fn;i++)
{
min=block[0];
for(j=0;j<bn;j++)
{
if((bflag[j]!=1)&&(file[i]<=block[j])&&(min>=block[j]))
{
min=block[j];
p=j;
}
}
fragment[i]=(Math.abs(file[i]-min));
bflag[j]=1;
System.out.println("Size of file "
+i+" is: "+file[i]);
System.out.println("Size of block "
+p+" is: "+block[p]);
System.out.println("Size of fragment "
+i+" is: "+fragment[i]);
}
}
public static void main(String args[]) throws
IOException
{
Bestfit bf=new Bestfit();
bf.accept();
bf.execute();
}
}
OUTPUT:
Enter the number of files
3
Enter the number of files
5
Enter the
size of each file
10
4
15
Enter the size of each block
64
10
5
20
30
Size of file 0 is:10
Size of block 1 is:10
Size of fragment 0 is:0
Size of file 1 is:4
Size of block 2 is:5
Size of fragment 1 is:1
Size of file 2 is:15
Size of block 3 is:20
Size of fragment 2 is:5
No comments:
Post a Comment