import java.io.*; import java.util.*; class cafe { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader bin=new BufferedReader(isr); int i,j,k=0,kk,run,counter; int n=6,m= 5,s=6; double se[]=new double[10]; int seed=12345; double r,iat=0,st=0,nat,wt=0,cwt; double p[]={0.0,0.05,0.4,0.65,0.8,0.9,0.97,1.0}; double at[]={0,0.5,1,1.5,2,2.5,3}; double f[]={0,0.05,0.3,0.65,0.85,1.0}; double t[]={1,2,3,4,5}; Random randy = new Random(12345); System.out.println("Enter the value of run:"); run=Integer.parseInt(bin.readLine()); for(j=0;j<=s;++j) { randy.nextInt(12345); counter=0; nat=0; cwt=0; for(i=1;i<=j;++i) { se[i]=0; } cwt=0; counter=0; for(counter=1;counter<=run;++counter) { r=randy.nextDouble()/32768.0; for(i=0;i<=n;++i) { if(r>p[i] && r<=p[i+1]) iat=at[i]; } r=randy.nextDouble()/32768.0; for(i=0;i<=m;++i) { if(r>f[i] && r<=f[i+1]) st=t[i]; } nat=nat+iat; double min=99.9; for(i=1;i<=j;++i) { if(se[i]<=min) { min=se[i]; k=i; } } if(nat<=min ) { se[k]=min+st; wt=(float)min-nat; } else se[k]=nat+st; cwt=(float)cwt+wt; } System.out.println("server"+" "+" Total arrival" +" " + "Average waiting time "); System.out.println(j+" "+counter+" "+(cwt/run)+" "); if((cwt/run)<=2) break; } } } /* OUTPUT C:\java progs>javac cafe.java C:\java progs>java cafe Enter the value of run: 30 server Total arrival Average waiting time 0 31 99.8999870300293 server Total arrival Average waiting time 1 31 14.5 server Total arrival Average waiting time 2 31 7.0 server Total arrival Average waiting time 3 31 4.5 server Total arrival Average waiting time 4 31 3.2666666666666666 server Total arrival Average waiting time 5 31 2.5 server Total arrival Average waiting time 6 31 2.0 C:\java progs> */
Program to implement Cafeteria problem in java
by
kiran
11:23 PM
About author: kiran
I am Freelance Facebook Application, Web and Flash Developer based in Mumbai, India. I have also worked on various other Platform like Twitter, Google, YouTube, LinkedIn etc. Apart from web and social app I also develop mobile apps and flash games.
I started this blog when I was Engineering to help some of my friends suffering from coding in labs. Later on Many more students love this blog and received many suggestion to improve it.