fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define MAX_VALUE 2000000001L
  6.  
  7. int main(void) {
  8. register long int counter,tmp, main_num, num_to_add, i1,i2;
  9. long int *array, smallest[3], size;
  10. FILE *input, *output;
  11.  
  12. if(!(input = fopen("operators.in","r"))) {
  13. printf("Failed to open operators.in\n");
  14. exit(EXIT_FAILURE);
  15. }
  16. if(!(output = fopen("operators.out","w"))) {
  17. printf("Failed to open operators.out\n");
  18. exit(EXIT_FAILURE);
  19. }
  20.  
  21. smallest[0] = MAX_VALUE;
  22.  
  23. fscanf(input,"%ld",&size);
  24.  
  25. if(!(array = (long int *) malloc(size * sizeof(long int)))) {
  26. printf("Failed to allocate memory for input\n");
  27. exit(EXIT_FAILURE);
  28. }
  29.  
  30. array[0] = 0;
  31. counter = 1;
  32. fscanf(input, "%ld", &array[0]);
  33.  
  34. do {
  35. fscanf(input, "%ld", &array[counter]);
  36. fscanf(input, "%ld", &array[counter + 1]);
  37. i1 = labs(array[0] + array[counter]);
  38. i2 = labs(array[0] + array[counter + 1]);
  39. counter += 2;
  40. }while(counter - 2 < size && i1 > i2);
  41.  
  42.  
  43. array = (long int *) realloc((long int *) array, ((size = counter) * sizeof(long int)));
  44.  
  45. for(main_num = 0; main_num < size; main_num++) {
  46. for(num_to_add = main_num + 1; num_to_add < size; num_to_add++) {
  47. if((tmp = (labs(((int ) array[main_num] + array[num_to_add])))) < smallest[0]) {
  48. smallest[0] = tmp;
  49. smallest[1] = array[main_num];
  50. smallest[2] = array[num_to_add];
  51. }
  52. }
  53. }
  54.  
  55. fprintf(output,"%ld %ld",smallest[1],smallest[2]);
  56.  
  57. free(array);
  58.  
  59. exit(EXIT_SUCCESS);
  60. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty