Está en la página 1de 5

Problemele de eficienta(timp si spatiu)

Ifstream f(“date.in”):

I. Se dau n numere
a. Prelucrarea numerelor independente unele fata de altele
Mod general de gandire
int n, x,i;
f>>n;
for( i=0; i<n; i++)
{
f>>x;
prelucrez x;
}

b. Prelucrez perechi de numere alaturate


int n, x, y,i;
f>>n;
f>>x;
for( i=1; i<n; i++)
{
f>>y;
prelucrez x;
prelucrez y;
procesez rezultatelede la x si y
x=y;
}

c. Prelucrez triplete de numere alaturate


int n, x, y,z,i;
f>>n;
f>>x>>y;
for( i=2; i<n; i++)
{
f>>z;
prelucrez x;
prelucrez y;
prelucrez z;
procesez rezultatele de la x si y si z
x=y;
y=z;
}
II. Se dau numere (in fisier sunt maxim 100000 de numere)
a. Prelucrez elemente independente
int x;
while(f>>x)
{
prelucrez x;
procesez rezultatul din x;
}

b. Prelucrez perechi de numere alaturate


int x,y;
f>>x;
while(f>>y)
{
prelucrez x;
prelucrez y;
procesez rezultatul din x si y;
x=y;
}

c. Prelucrez triplete de numere alaturate

int x,y,z;
f>>x>>y;
while(f>>z)
{
prelucrez x;
prelucrez y;
prelucrez z;
procesez rezultatul din x si y si z;
x=y;
y=z;
}
Algoritmi eficienti

1. Se dau numere. Sa se determine maximul si numarul de aparitii


I. Citire cu n numere
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("date.in");
int main()
{
int n, x, vmax=-2000000000, ap,i;
f>>n;
for(i=0; i<n; i++)
{
f>>x;
if(x>vmax)
{
vmax=x;
ap=1;
}
else
if(x==vmax)
ap++;
}
cout<<vmax<<" "<<ap;
return 0;
}
II. Citire numere pana cand se termina din fisier
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("date.in");
int main()
{
int x, vmax=-2000000000, ap;
while (f>>x)
if(x>vmax)
{
vmax=x;
ap=1;
}
else
if(x==vmax)
ap++;
cout<<vmax<<" "<<ap;
return 0;
}
2. Se dau numere. Sa se determine cele mai mari trei numere.
I. Cu n numere
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("date.in");
int main()
{
int n, x, max1,max2,max3, i;
f>>n;
max1=max2=max3=-2000000000;
for(i=0; i<n; i++)
{
f>>x;
if(x>max1)
{
max3=max2;
max2=max1;
max1=x;
}
else
if(x>max2)
{
max3=max2;
max2=x;
}
else
if(x>max3)
max3=x;
}
cout<<max1<<" "<<max2<<" "<<max3;
return 0;
}
II. Cu numere pana cand se termina din fisier
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("date.in");
int main()
{
int x, max1,max2,max3;
max1=max2=max3=-2000000000;
while (f>>x)
{
if(x>max1)
{
max3=max2;
max2=max1;
max1=x;
}
else if(x>max2)
{
max3=max2;
max2=x;
}
else if(x>max3)
max3=x;
}
cout<<max1<<" "<<max2<<" "<<max3;
return 0;
}

Vectori de frecventa sau de aparitie

Intr-un vector de frecventa pozitia din vector reprezinta valoarea(numarul) analizata, iar valoarea
din vectorul de frecventa reprezinta numarul de aparitii al valorii reprezentate de pozitie, adica
daca am vectorul ap[10] inseamna ca reprezint cifrele de la 0 la 9 si generalizand ap[i] reprezinta
numarul de aparitii a lui i.

ap 12 0 0 1 3 0 0 0 2 4
0 1 2 3 4 5 6 7 8 9

numarul 0 apare de 12 ori


numarul 1 apare de 0 ori
numarul 2 apare de 0 ori numarul i apare de ap[i] ori
………….
Numarul 8 apare de 2 ori
Numarul 9 apare de 4 ori

Sunt 2 etape de lucru cu vectorul de frecventa: in prima etapa se formeaza


vectorul de frecventa, iar in a 2-a etapa se prelucreaza vectorul de frecventa.

También podría gustarte