Glaas2

Alg_Est

Jan 25th, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. //////////////////////////////////////// ej 1
  2.  
  3. //!Sumatoria de valores en un intervalo dado de un arreglo m veces
  4. //!
  5. #include<ctime>
  6. #include<iostream>
  7.  
  8. int main( ) {
  9.    int n;
  10.    std::cin >> n;
  11.  
  12.    int arr[n];
  13.    for (int i = 0; i < n; ++i) {
  14.       std::cin >> arr[i];
  15.    }
  16.  
  17.    int prefijo[n];      //arreglo de suma de prefijos (suma de acumulados)
  18.    for (int i=0;i<n;++i)
  19.    {
  20.         prefijo[i]=arr[i]+(i !=0?prefijo[i-1]:0);
  21.    }
  22.  
  23.    int m;
  24.    std::cin >> m;
  25.  
  26.    for (int i = 0; i < m; ++i) {
  27.       int x, y;
  28.       std::cin >> x >> y;
  29.  
  30.       int res = prefijo[y];
  31.       if(x!=0)
  32.         res=prefijo[x-1];
  33.       std::cout<<res<<"\n";
  34.    }
  35.  
  36. }
  37.  
  38. /////////////////////////////////////////////////////////////////////////////////////////////////////
  39.  
  40. //************************************** primo o no *******************************************/
  41. #include <iostream>
  42. #include <math.h>
  43.  
  44. using namespace std;
  45.  
  46. bool es_primo(int n)
  47. {
  48.         if (n==1)
  49.         {
  50.             return false;
  51.         }
  52.  
  53.         if (n>2 && n%2==0)
  54.         {
  55.             return false;
  56.         }
  57.     int raiz=sqrt(n);
  58.         for(int i=3; i<=raiz; i+=2)
  59.         {
  60.             if (n%i == 0)
  61.             {
  62.                 return false;
  63.             }
  64.         }
  65.  
  66.         return true;
  67. }
  68.  
  69.  
  70.  
  71. int main()
  72. {
  73.     int n;
  74.     cin >> n;
  75.     cout << es_primo(n) << "\n";
  76.     return 0;
  77. }
  78. //***********************************************************/
  79.  
  80. ///////////////////////////////////////* Busqueda lineal *////////////////////////////////////////////////
  81. #include <iostream>
  82. #include <algorithm>
  83.  
  84. using namespace std;
  85.  
  86.  
  87. /*********
  88. int* busca(int* pos, int* fin, int k) // pos: posicion actual, fin: ap al elemento fuera del arreglo, k: valor a buscar
  89. {
  90.  
  91.     while (pos != fin && *pos!= k )
  92.     {
  93.         ++pos;
  94.     }
  95.  
  96.     return pos;
  97. }*/
  98.  
  99. int main()
  100. {
  101.  
  102.     int n;
  103.     cin >> n; // tam del arreglo
  104.  
  105.     int arr[n]; // llenado del arreglo
  106.         for(int i =0; i < n ; ++i)
  107.         {
  108.             cin >> arr[i];
  109.         }
  110.  
  111.     int k; // entero a buscar
  112.     cin >> k;
  113.  
  114.     int* p = busca(arr, &arr[n], k); //para usar el std::find se usa find(...); y se borra la implem de la funcion busca
  115.     if(p == &arr[n])
  116.         cout << "no existe el elemento\n";
  117.     else
  118.     {
  119.         cout << "el elemento " << *p << " existe en la pos " << p-arr << " del arreglo\n";
  120.     }
  121.  
  122.  
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment