Măsurarea timpului de execuţie
#include<iostream>
using namespace std;
int main()
{
clock_t start, stop ;
start = clock();
//instructiuni
stop = clock() ;
cout<<(double)(stop-start)/CLOCKS_PER_SEC ;
return 0 ;
}
Lucru cu vectori
#include<iostream>
using namespace std;
int a[1000];
int main()
{
int n,poz,maxim,i,s,suma;
cout<<"\n n= "; cin>>n;
s=0;
for(i=0;i<n;i++)
{
cin>>a[i];
}
maxim=a[i];
poz=0;
suma=0;
for(i=0;i<n;i++)
{
if (a[i]%2==0)
s=s+a[i];
if (maxim<a[i])
{
maxim=a[i];
poz=i;
}
if (i%2==1)
suma=suma+ a[i];
}
cout << "suma pozitiilor impare=" << suma <<" ";
cout << "suma cifrelor pare=" << s <<" ";
cout <<"maxim=" <<maxim<< " "<< "pozitia="<<poz;
}
Ciurul lui Eratostene
ajuta la generarea numerelor prime pana la valoarea numarului citit (s)
#include<fstream>
using namespace std ;
int a[1001];
int main ()
{
int s,i,j;
ifstream fin("ciur.in");
fin>>s;
fin.close();
for(i=4;i<=s;i+=2)
a[i]=1;
for(i=3;i*i<=s;i+=2)
{
if(a[i]==0)
for(j=i*i;j<=s;j=j+2*i)
a[j]=1;
}
ofstream fout("ciur.out");
for(i=2;i<=s;i++)
if(a[i]==0)
fout<<i<<" ";
fout.close();
return 0;
}
Subsecventa de suma maxima
#include <fstream>
using
namespace
std;
int
a[6000002];
int
main ()
{
int
n,i,s,st,dr,maxim,poz;
fstream fin(
"ssm.in"
);
fin>>n;
for
(i=1; i<=n; i++)
fin>>a[i];
s=a[1];
maxim=a[1];
st=1;
poz=1;
dr=1;
if
(s<0)
{
s=0;
poz=2;
}
for
(i=2; i<=n; i++)
{
s=s+a[i];
if
(s>maxim)
{
maxim=s;
dr=i;
st=poz;
if
(s<0)
{
s=0;
poz=i+1;
}
}
ofstream fout(
"ssm.out"
);
fout<<maxim<<
" "
<<st<<
" "
<<dr<<
"\n"
;
fout.close();
return
0;
}
Lucru cu matrice
L=linia
C=coloana
#include<iostream>
using namespace std;
int a[102][102], L, C;
int main()
{
int i, j;
cin >>L>> C;
for (i = 1; i <= L; i++)
for (j = 1; j <= C; j++)
cin >>a[i][j];
for (i = 1; i <= L; i++)
{
for (j = 1; j <= C; j++) cout << a[i][j] << " ";
cout << "\n";
}
// suma elementelor de pe linia 3
int s = 0;
for (j = 1; j <= C; j++)
s += a[3][j];
cout << s << "\n";
// suma elem de pe coloana 4
s = 0;
for (i = 1; i <= L; i++)
s += a[i][4];
cout << s << "\n";
return 0;
}