upsidedown

optitape(daa)

Mar 13th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include<iostream.h>
  2. #include<iomanip.h>
  3.  
  4. int fact(int);
  5.  
  6. int main()
  7. {
  8.     cout<<"enter total number of tapes\n";
  9.     int n,j,a[10],c,b[10],sum;
  10.     cin>>n;
  11.     cout<<"enter the time taken for indivisual tapes to play\n";
  12.     for(j=0;j<=n-1;j++)
  13.         cin>>a[j];
  14.     cout<<"the tapes are\n";
  15.     for(int k=1;k<=n;k++)
  16.         cout<<setw(3)<<"T"<<k;
  17.     cout<<"\nthe combinations are:\n";
  18.     k=0;
  19.     for(int i=0;i<=(fact(n)-1)/2;i++)
  20.     {
  21.         sum=0;
  22.         c=n;
  23.         for(j=i+1;j<=n;j++)
  24.         {
  25.             cout<<setw(3)<<"T"<<j;
  26.             sum=sum+(c--)*a[j-1];
  27.         }
  28.         for(j=1;j<=i%n;j++)
  29.         {
  30.             cout<<setw(3)<<"T"<<j;
  31.             sum=sum+(c--)*a[j-1];
  32.         }
  33.         cout<<setw(14)<<" Time taken is="<<sum<<endl;
  34.         b[k]=sum;
  35.         k++;
  36.         sum=0;
  37.         c=n;
  38.         for(j=i%n;j>=1;j--)
  39.         {
  40.             cout<<setw(3)<<"T"<<j;
  41.             sum=sum+(c--)*a[j-1];
  42.         }
  43.         for(j=n;j>=i+1;j--)
  44.         {
  45.             cout<<setw(3)<<"T"<<j;
  46.             sum=sum+(c--)*a[j-1];
  47.         }
  48.         cout<<setw(14)<<" Time taken is="<<sum<<endl;
  49.         b[k]=sum;
  50.         k++;
  51.     }
  52.     int t;
  53.     k--;
  54.     t=b[0];
  55.     for(j=0;j<=5;j++)
  56.     {
  57.         if(t>b[j])
  58.             t=b[j];
  59.     }
  60.     cout<<"the minimal optimum solution is:\n"<<t<<endl;
  61.     return 0;
  62. }
  63.  
  64. int fact(int x)
  65. {
  66.     int facto=1;
  67.     if(x==1||x==0)
  68.         return 1;
  69.     else
  70.     {
  71.        
  72.         return x*fact(x-1);
  73.     }
  74. }
  75. OUTPUT:
  76. enter total number of tapes
  77. 3
  78. enter the time taken for indivisual tapes to play
  79. 5
  80. 10
  81. 3
  82. the tapes are
  83.  T1  T2  T3
  84. the combinations are:
  85.  T1  T2  T3 Time taken is=38
  86.  T3  T2  T1 Time taken is=34
  87.  T2  T3  T1 Time taken is=41
  88.  T1  T3  T2 Time taken is=31
  89.  T3  T1  T2 Time taken is=29
  90.  T2  T1  T3 Time taken is=43
  91. the minimal optimum solution is:
  92. 29
Advertisement
Add Comment
Please, Sign In to add comment