Advertisement
Pridexs

PECAS - METODO 2 (CORRETO)

Jun 13th, 2014
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string>
  5. #include <utility>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <map>
  9. #include <math.h>
  10.  
  11. using namespace std;
  12.  
  13. int compare (const void *a, const void *b) {
  14.     return ( *(int*)a - *(int*)b );
  15. }
  16.  
  17. int main()
  18. {
  19.     int x, n;
  20.     int cent[1000001];
  21.     int cont;
  22.     int menor, maior;
  23.     int size;
  24.     int cont3;
  25.     int diferenca;
  26.     bool achou;
  27.  
  28.     while (scanf ("%d %d", &x, &n) != EOF) {
  29.         x *= 10000000;
  30.         size = 0;
  31.         achou = false;
  32.         menor = 0;
  33.         maior = 0;
  34.         for (int i = 0; i < n; i++) {
  35.             scanf("%d", &cont);
  36.             cent[i] = cont;
  37.         }
  38.  
  39.         qsort(cent, n, sizeof(int), compare);
  40.  
  41.         size = (n-1);
  42.         cont3 = 0;
  43.         for (int i = size; i > cont3; i--) {
  44.             if (cent[i] + cent[cont3] == x) {
  45.                 maior = cent[i];
  46.                 menor = cent[cont3];
  47.                 break;
  48.             } else if (cent[i] + cent[cont3] < x) {
  49.                 cont3++;
  50.                 i = size+1;
  51.             }
  52.         }
  53.  
  54.         if (!menor && !maior) {
  55.             printf("PERIGO\n");
  56.         } else {
  57.             printf("SIM %d %d\n", menor, maior);
  58.         }
  59.     }
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement