Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////// ej 1
- //!Sumatoria de valores en un intervalo dado de un arreglo m veces
- //!
- #include<ctime>
- #include<iostream>
- int main( ) {
- int n;
- std::cin >> n;
- int arr[n];
- for (int i = 0; i < n; ++i) {
- std::cin >> arr[i];
- }
- int prefijo[n]; //arreglo de suma de prefijos (suma de acumulados)
- for (int i=0;i<n;++i)
- {
- prefijo[i]=arr[i]+(i !=0?prefijo[i-1]:0);
- }
- int m;
- std::cin >> m;
- for (int i = 0; i < m; ++i) {
- int x, y;
- std::cin >> x >> y;
- int res = prefijo[y];
- if(x!=0)
- res=prefijo[x-1];
- std::cout<<res<<"\n";
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////
- //************************************** primo o no *******************************************/
- #include <iostream>
- #include <math.h>
- using namespace std;
- bool es_primo(int n)
- {
- if (n==1)
- {
- return false;
- }
- if (n>2 && n%2==0)
- {
- return false;
- }
- int raiz=sqrt(n);
- for(int i=3; i<=raiz; i+=2)
- {
- if (n%i == 0)
- {
- return false;
- }
- }
- return true;
- }
- int main()
- {
- int n;
- cin >> n;
- cout << es_primo(n) << "\n";
- return 0;
- }
- //***********************************************************/
- ///////////////////////////////////////* Busqueda lineal *////////////////////////////////////////////////
- #include <iostream>
- #include <algorithm>
- using namespace std;
- /*********
- int* busca(int* pos, int* fin, int k) // pos: posicion actual, fin: ap al elemento fuera del arreglo, k: valor a buscar
- {
- while (pos != fin && *pos!= k )
- {
- ++pos;
- }
- return pos;
- }*/
- int main()
- {
- int n;
- cin >> n; // tam del arreglo
- int arr[n]; // llenado del arreglo
- for(int i =0; i < n ; ++i)
- {
- cin >> arr[i];
- }
- int k; // entero a buscar
- cin >> k;
- int* p = busca(arr, &arr[n], k); //para usar el std::find se usa find(...); y se borra la implem de la funcion busca
- if(p == &arr[n])
- cout << "no existe el elemento\n";
- else
- {
- cout << "el elemento " << *p << " existe en la pos " << p-arr << " del arreglo\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment