SHARE
TWEET

Untitled

a guest Mar 24th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. FILE *fin;
  4. float L;
  5. float minLength,controlLength;
  6. int pointer;
  7. int N,controlNumber;
  8. int minNumber;
  9. struct Book *d;
  10.  
  11. struct Book
  12. {
  13.     float length;
  14.     int number;
  15. };
  16.  
  17. int main(void)
  18. {
  19.     fin = fopen("books.dat","r");
  20.     fscanf(fin,"%f",&L);
  21.     fscanf(fin,"%d",&N);
  22.    
  23.     d = (struct Book*)malloc(sizeof(struct Book) * N);
  24.    
  25.     for (int i = 0; i < N;i++)
  26.     {
  27.         fscanf(fin,"%f",&d[i].length);
  28.         d[i].number = i;
  29.         //printf("NUMBERCHECK:%d",d[i].number);
  30.     }
  31.    
  32.     do{
  33.         minLength = d[0].length;
  34.         //printf("MINLENGTHCHECK:%f\n",minLength);
  35.         for (int i = 0; i < N; i++){
  36.             if (d[i].length <= minLength){
  37.                 pointer = i;
  38.                 minLength = d[i].length;
  39.                 minNumber = d[i].number;
  40.                 //printf("NUMBERCHECK:%d\n",d[i].number);
  41.             }
  42.         }
  43.         //N--;
  44.         //L = L - minLength;
  45.         /*if ((N>0) && (L>0)){
  46.             printf("BOOK #:%d LENGTH:%f\n",minNumber,minLength);
  47.             controlLength += minLength;
  48.             controlNumber++;
  49.         }*/
  50.         for (int i = pointer; i < N - 1; i++){
  51.             d[i] = d[i+1];
  52.         }
  53.         N--;
  54.         L = L - minLength;
  55.         if ((N>=0) && (L>=0)){
  56.             printf("BOOK #:%d LENGTH:%f\n",minNumber+1,minLength);
  57.             controlLength += minLength;
  58.             controlNumber++;
  59.         }
  60.  
  61.     } while((L >= 0) && (N>=0));
  62.    
  63.     printf("\nN=%d\nL=%f\n",controlNumber,controlLength);
  64.  
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top