Advertisement
greatbn

N người N Việc

Sep 1st, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include<graphics.h>
  5. int A[50][50],column[50],x[50],n,max_hs = 0,count=0,kq[50][50];
  6. void save_()
  7. {
  8.     for(int i = 0;i<n;i++)
  9.         kq[count][i] = x[i];   
  10.     count++;
  11. }
  12. void nhap()
  13. {
  14.     printf("Nhap vao so nguoi va so viec: ");
  15.     scanf("%d",&n);
  16.    
  17.     for(int i=0;i<n;i++)
  18.     {
  19.         for(int j=0;j<n;j++)
  20.         {
  21.             printf("Nguoi thu %d lam viec %d voi hieu suat: ", i+1,j+1);
  22.             scanf("%d",&A[i][j]);
  23.         }
  24.     }
  25.     for(int i=0;i<n;i++)
  26.         x[i] = 0;
  27. }
  28. void hs_max(int k)
  29. {
  30.     int i,j;
  31.     if(k>=n)// dieu kien de in
  32.     {
  33.         int hieusuat = 0;
  34.         for(i=0;i<n;i++)
  35.         {
  36.             hieusuat += A[i][x[i]];    
  37.         }
  38.         if(hieusuat>=max_hs)
  39.             max_hs = hieusuat;  
  40.     }
  41.     else
  42.     {
  43.         for(j=0;j<n;j++)
  44.         {
  45.             if(column[j]==0)
  46.             {
  47.                 x[k]=j;
  48.                 column[j]=1;
  49.                 hs_max(k+1);
  50.                 column[j]=0;
  51.             }
  52.         }
  53.        
  54.     }
  55. }
  56. void print_hs_max(int k)
  57. {
  58.     int i,j;
  59.     if(k>=n)// dieu kien de in
  60.     {
  61.         int hieusuat = 0;
  62.         int max[50][50];
  63.         for(i=0;i<n;i++)
  64.         {
  65.             hieusuat += A[i][x[i]];
  66.         }
  67.         if(hieusuat == max_hs) {
  68.             save_();
  69.             }    
  70.     }
  71.     else
  72.     {
  73.         for(j=0;j<n;j++)
  74.         {
  75.             if(column[j]==0)
  76.             {
  77.                 x[k]=j;
  78.                 column[j]=1;
  79.                 print_hs_max(k+1);
  80.                 column[j]=0;
  81.             }
  82.         }
  83.     }
  84. }
  85.  
  86. main()
  87. {
  88.     nhap();
  89.     int x1=70,y2=110;
  90.     int p = 80,q = 120;
  91.     char msg[50];
  92.     // khoi tao do hoa
  93.     int sc = DETECT,mode;
  94.     initgraph(&sc,&mode,"C:\\BC5\\BGI");
  95.    
  96.     // Chay ham tim hieu suat lon nhat
  97.     hs_max(0);
  98.     // chay ham sap xep cong viec
  99.     print_hs_max(0);
  100.     // in ra man hinh so cach chon
  101.     settextstyle(1,HORIZ_DIR,1);
  102.     sprintf(msg,"Co %d cach chon sao cho hieu suat lon nhat",count);
  103.      outtextxy(60,40,msg);
  104.      // in ra man hinh N và V
  105.  
  106.    // in ra cach tim duoc
  107.    for(int c = 0;c<count;c++)
  108.    {
  109.       for(int i =0 ;i<n;i++)
  110.         {
  111.             sprintf(msg,"V%d",i+1);
  112.             setcolor(WHITE);
  113.             settextstyle(1, HORIZ_DIR, 1);
  114.             outtextxy(x1,90,msg);
  115.             x1=x1+70;
  116.             sprintf(msg,"N%d",i+1);
  117.             setcolor(WHITE);
  118.             settextstyle(1,HORIZ_DIR,1);
  119.             outtextxy(30,y2,msg);
  120.             y2=y2+80;
  121.       }
  122.         settextstyle(1,HORIZ_DIR,2);
  123.         sprintf(msg,"Cach %d. Nhan phim bat ky de xem cach tiep theo",c+1);
  124.         outtextxy(70,60,msg);
  125.         for(int i=0;i<n;i++)
  126.     {
  127.         for(int j=0;j<n;j++){
  128.             sprintf(msg,"%d",A[i][j]);
  129.             if(j==kq[c][i])
  130.             {
  131.                 setcolor(RED);
  132.                 settextstyle(1, HORIZ_DIR, 1);
  133.                 outtextxy(p,q,msg);
  134.             }
  135.             else{
  136.                 setcolor(WHITE);
  137.                 settextstyle(1, HORIZ_DIR, 1);
  138.                 outtextxy(p,q,msg);
  139.             }
  140.             p = p+70;
  141.            
  142.         }
  143.         p = 90;
  144.         q = q +80;
  145.     }
  146.     getch();
  147.     cleardevice();
  148.     p=80;
  149.     q=120;
  150.      x1=70;
  151.      y2=110;
  152.    }
  153.    
  154.     getch();
  155.     closegraph();
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement