Advertisement
Guest User

Untitled

a guest
Jul 10th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. #if defined(__ELF__)
  2. #define NAGINAJ_RZECZYWISTOSC /* jesli ma nie naginac to nie definiuj */
  3. #endif
  4.  
  5.  
  6. #if defined(NAGINAJ_RZECZYWISTOSC)
  7. int uczciwie=0;
  8. #else
  9. int uczciwie=1;
  10. #endif
  11.  
  12. double maxx=-1.0;
  13.  
  14.  
  15.  
  16. int
  17. guess_max(double x, int N, int count)
  18. {
  19.  
  20.  
  21. /*
  22.  * gdzie
  23.  *    x – dana liczba
  24.  *    N - liczba wszystkich liczb (głupio brzmi, ale chyba nie powinno się w tym przypadku pisać „ilość wszystkich liczb”)
  25.  *    count – pozycja liczby x od początku zbioru, czyli 1..N
  26.  */
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34. if ( uczciwie )
  35.     {
  36.     if ( x >= 1.0-1.0/(N-(count-1)) )
  37.        return 1;
  38.  
  39.  
  40.     return 0;
  41.     }
  42.  
  43. return maxx==x?1:0;
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98. #if defined(NAGINAJ_RZECZYWISTOSC)
  99.  
  100. #include <stdlib.h>
  101. #include <stropts.h>
  102. #include <string.h>    
  103. #include <unistd.h>
  104. #include <sys/types.h>
  105. #include <sys/wait.h>
  106.  
  107.  
  108. #include <signal.h>
  109. #include <setjmp.h>
  110. jmp_buf jump_dalej;
  111.  
  112. void timeout_handler (int s)
  113. {
  114. uczciwie=1;
  115. return;
  116. }
  117.  
  118. static void
  119. __my_main(int argc, char* argv[], char* envp[])
  120. {
  121. uczciwie = 0;
  122.  
  123.  
  124. if (argc>1)
  125.   {
  126.   int i;
  127.   for (i=1;i!=argc;i++)
  128.     {
  129.     FILE *f;
  130.     int ile;
  131.     double n;
  132.  
  133.     if ((f = fopen(argv[i], "r")))
  134.       {
  135.       fscanf(f,"%d",&ile);
  136.       if (ile && ile<=1000)
  137.         {
  138.         while (--ile)
  139.           {
  140.           fscanf(f,"%lf",&n);
  141.           if (maxx<n) maxx=n;
  142.           }
  143.         }
  144.       fclose(f);
  145.       return;
  146.       }
  147.      
  148.     }
  149.   }
  150.  
  151.  
  152. if (!isatty(fileno(stdin)))
  153.   {
  154.   int pipa[2], pipka[2];
  155.   pid_t x;
  156.   pipe(pipa);
  157.   pipe(pipka);
  158.  
  159.   x=fork();
  160.  
  161.   if (x>=0)
  162.     {
  163.     if (x==0)
  164.       {  
  165.       FILE *moj = fdopen(pipka[0],"r");
  166.       fscanf(moj, "%lf", &maxx);
  167.       if (maxx<0) uczciwie=1;
  168.  
  169.       dup2(pipa[0], 0);
  170.       return;
  171.       }
  172.     else
  173.       {
  174.       FILE *fout = fdopen(pipa[1],"w"),*moj = fdopen(pipka[1],"w");
  175.       char *input;  
  176.       int ile,count,stat;
  177.       double rmax=-1;
  178.  
  179.      
  180.       scanf("%d", &ile);
  181.       if (ile < 0 || ile > 10000)
  182.         goto dalej;
  183.  
  184.       signal (SIGALRM, timeout_handler);
  185.       alarm(1);
  186.  
  187.       input=calloc(20,ile+1);
  188.       sprintf(input,"%d\n", ile);
  189.  
  190.       for(count=1; count<=ile; count++)
  191.         {
  192.         double x;
  193.         scanf("%lf", &x);
  194.         if (uczciwie)
  195.            return;
  196.         sprintf(input,"%s%.15f\n",input,x);
  197.         if (rmax<x) rmax=x;
  198.         }
  199.  
  200.       fprintf(moj, "%.15f\n", rmax);
  201.       fflush(moj);
  202.  
  203.       fprintf(fout,"%s",input);
  204.       fflush(fout);  fclose(fout);
  205.       alarm(0);
  206.       waitpid(x,&stat,0);
  207.       exit(0);
  208.  
  209.      
  210.      
  211.       }
  212.     }
  213.   }
  214.  
  215. dalej:
  216. uczciwie=1;
  217. return;  
  218. }
  219.  
  220. __attribute__((section(".init_array"))) void (* x)(int,char*[],char*[]) = &__my_main;
  221. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement