Documente online.
Zona de administrare documente. Fisierele tale
Am uitat parola x Creaza cont nou
 HomeExploreaza
upload
Upload




Algoritmi

Informatica


Algoritmi

1.1. Notiunea de algoritm, caracteristici

Sirul lui Fibonacci Sirul lui Fibonacci in vector Elementele dintr-un vector care fac parte din sirul lui Fibonacci Numere perfecte Cel mai mare divizor comun al unor numere (algoritmul lui Euclid) Cel mai mic multiplu comun al doua numere Verificare daca un numar este prim Descompunerea unui numar in factori primi Cifra de rang Cifra de control a unui numar Verificare daca numerele de la 1 la n sant palindroame Intercalarea a doi vectori dupa poziti 22522i84w a k Suma a doi vectori considerati numere Regula Paharelor (interschimbarea valorilor variabilelor) Metoda Bulelor (ordonarea elementelor in tablouri) Separarea intr-un vector a elementelor pare de cele impare Interschimbarea a doua linii dintr-o matrice Eliminarea unei coloane dintr-o matrice Construirea transpusei unei matrici Sumele obtinute de pe conturul unui patrat din matrice Pozitiile calului pe tabla de sah Transformarea literelor mari in litere mici Verificare daca doua cuvinte rimeaza Frecventa valorilor in vector (de cate ori apare fiecare valoare) Generarea patratului magic Numarul de cifre al unui numar Maximul dintr-un sir (cea mai mare valoare) Minimul dintr-un sir (cea mai mica valoare) Suma cifrelor unui numar



. Sirul lui Fibonacci
var n,i,a,b,s:integer;
begin
write('n='); readln(n);
if n=1 then write('1') else if n=2 then write('1 1') else
 begin
  write('1 1') ; a:=1; b:=1;
  for i:=1 to n-2 do
   begin s:=a+b; write(s,' '); a:=b; b:=s; end;
 end;
readln; end.

. Sirul lui Fibonacci in vector
var v:array[1..100] of integer;
       n,i:integer;
begin
write('n='); readln(n);
v[1]:=1; v[2]:=1;
for i:=3 to n do v[i]:=v[i-1]+v[i-2];
for i:=1 to n do write(v[i],' ');
readln; end.

. Elementele dintr-un vector care fac parte din sirul lui Fibonacci
var v:array[1..100] of integer;
       x,y,z,n,i:integer;
begin
write('n='); readln(n);
for i:=1 to n do
 begin write('v[',i,']='); readln(v[i]); end;
for i:=1 to n do
 begin
  x:=1; y:=1; z:=x+y;
  while z<v[i] do
   begin x:=y; y:=z; z:=x+y; end;
  if z=v[i] then write(v[i],' ');
 end;
readln; end.

. Numere perfecte (suma divizorilor=numarul; Ex: 6=1+2+3; 28=1+2+4+7+14)
var i,j,n:integer; g:boolean;
begin
write('n='); readln(n);
for i:=1 to n do
 begin
  s:=0;
  for j:=1 to i-1 do
   if i mod j=0 then s:=s+j;
  if s=i then write(i,' ');
 end;
readln; end.

. Cel mai mare divizor comun al unor numere (algoritmul lui Euclid)
var x,y,i,n,r:integer;
begin
write('n=') ; readln(n);
if n<2 then write('Dati un numar mai mare') else
 begin
  writeln('x='); readln(x);
  for i:=1 to n-1 do
   begin
    write('y='); readln(y);
    r:=x mod y;
    while r<>0 do
     begin x:=y; y:=r; r:=x mod y; end;
    x:=y;
   end;
  write(x);
 end;
readln; end.

. Cel mai mic multiplu comun al doua numere
var x,y,z,a,b:integer;
begin
write('x=') ; readln(x); a:=x;
write('y=') ; readln(y); b:=y;
z:=x mod y;
while z<>0 do begin x:=y; y:=z; z:=x mod y; end;
write('cmmmc= ',(a*b)/y:3:3);
readln; end.

. Verificare daca un numar este prim
var n,i:integer; g:boolean;
begin
write('n='); readln(n);
if n=1 then write(n,' nu este numar prim') else
 begin
  g:=true;
  for i:=2 to trunc(sqrt(n)) do
   if n mod i=0 then g:=false;
 end;
if g then write(n,' este numar prim')
      else write(n,' nu este numar prim');
readln; end.

. Descompunerea unui numar in factori primi
var n,d:integer;
begin
write('n='); readln(n);
d:=2;
repeat
 while (n mod d=0) do
  begin

   writeln(n:8,#179,d);
   n:=n div d;
  end;
 d:=d+1;
until n=1;
write(n:8,#179) ;
readln; end.

. Cifra de rang
var n,k,i:integer;
begin
write('n='); readln(n);
write('k='); readln(k);
for i:=1 to k-1 do n:=n div 10;
if n<>0 then write('cifra de rang: ',n mod 10)
    else write('k este prea mare');
readln; end.

. Cifra de control a unui numar
var x,n,s:integer;
begin
write('n='); readln(n);
while n>9 do
 begin
  s:=0; x:=n;
  while x<>0 do begin
          s:=s+x mod 10;
          x:=x div 10;
                 end;
  n:=s;
 end;
write('cifra de control: ',n);
readln; end.

. Verificare daca numerele de la 1 la n sant palindroame (egale cu inversul lor - 121=121, 123<>321)
var n,m,i,x:integer;
begin
write('n='); readln(n);
for i:=1 to n do
 begin
  x:=i; m:=0;
  repeat
   m:=m*10+x mod 10;
   x:=x div 10;
  until x=0;
  if m=i then write(i,' ');
 end;
readln; end.

. Intercalarea a doi vectori dupa pozitia k
var v,w:array[1..100] of integer;
      i,k,n,m:integer;
begin
write('n='); readln(n);
for i:=1 to n do
  begin write('v[',i,']='); readln(v[i]); end;
write('m='); readln(m);
for i:=1 to m do
 begin write('w[',i,']='); readln(w[i]); end;
write('k='); readln(k);
for i:=1 to n-k do w[m+i]:=v[k+i];
m:=m+n-k;
for i:=1 to m do v[k+1]:=w[i];
for i:=1 to k+m do write(v[i],' ');
readln; end.

. Suma a doi vectori considerati numere (v=7,4,9 ==» nr=749)
var v,w,s:array[1..100] of integer;
      i,j,n,m,t,q:integer;
begin
write('n='); readln(n);
for i:=1 to n do
 begin write('v[',i,']='); readln(v[i]); end;
write('m='); readln(m);
for i:=1 to n do
  begin write('w[',i,']='); readln(w[i]); end;
t:=0;
if m>n then q:=m else q:=n;
for i:=q downto 1 do
 begin
  s[i]:=(t+v[i]+w[i]) mod 10;
  t:=(t+v[i]+w[i]) div 10;
 end;
if t<>0 then
for i:=q downto 1 do s[i+1]:=s[i];
s[1]:=t; q:=q+1;
for i:=1 to q do write(s[i],' ');
readln; end.

. Regula Paharelor (interschimbarea valorilor variabilelor)
var u,v,aux:integer;
begin
write('u='); readln(u);
write('v='); readln(v);
aux:=u;
u:=v;
v:=u;
writeln('u=',u);
writeln('v=',v);
readln; end.

. Metoda Bulelor (ordonarea elementelor in tablouri) - ordonarea crescatoare
var v:array[1..100] of integer;
      i,n,aux:integer;
      g:boolean;
begin
write('n='); readln(n);
for i:=1 to n do
 begin write('v[',i,']='); readln(v[i]); end;
repeat
 g:=true;
 for i:=1 to n-1 do
 if v[i]>v[i+1] then
  begin
   aux:=v[i];
   v[i]:=v[i+1];
   v[i+1]:=aux;
   g:=false;
  end;
until g;
for i:=1 to n do write(v[i],' ');
readln; end.

. Separarea intr-un vector a elementelor pare de cele impare
var v:array[1..100] of integer;
      n,i,j.aux:integer;
      g:boolean;
begin
write('n='); readln(n);
for i:=1 to n do
 begin write('v[',i,']='); readln(v[i]); end;
repeat
 g:=false;
 for i:=1 to n do
  begin
   if v[i] mod 2<>0 then
    begin
     for j:=i+1 to n do if v[j] mod 2=0 then
      begin g:=true; aux:=v[i]; v[i]:=v[j]; v[j]:=aux; end;
     break;
    end;
  end;
 end;
until not g;
for i:=1 to n do write(v[i],' ');
readln; end.

. Interschimbarea a doua linii dintr-o matrice
var a:array[1..100,1..100] of integer;
      aux,i,n,m,l1,l2:integer;
begin
write('m='); readln(m);
write('n=');readln(n)
for i:=1 to m do
for j:=1 to n do
 begin write('a[',i,',',j,']='); readln(a[i,j]); end;
write('linia 1='); readln(l1);
write('linia 2='); readln(l2);
for j:=1 to n do
 begin aux:=a[l1,j]; a[l1,j]:=a[l2,j]; a[l2,j]:=aux; end;
for i:=1 to m do
 begin
  for j:=1 to n do write(a[i,j],' ');
  writeln;
 end;
readln; end.

. Eliminarea unei coloane dintr-o matrice
var a:array[1..100,1..100] of integer;
      m,n,c:integer;
begin
write('m='); readln(m);
write('n='); readln(n);
for i:=1 to m do
for j:=1 to n do
 begin write('a[',i,',',j,']='); readln(a[i,j]); end;
write('coloana='); readln(c);
for j:=c to n-1 do
for i:=1 to m do
 a[i,j]:=a[i,j+1];
for j:=1 to n-1 do
 begin
  for i:=1 to m do write(a[i,j],' ');
  writeln;
 end;
readln; end.

. Construirea transpusei unei matrici
var a,b:array[1..100] of integer;
      i,j,m,n:integer;
begin
write('n='); readln(n);
write('m='); readln(m);
for i:=1 to m do
for j:=1 to n do
 begin write('a[',i,',',j,']='); readln(a[i,j]); end;
for i:=1 to m do
for j:=1 to n do
 b[i,j]:=a[m-i+1,j];
for j:=1 to n-1 do
 begin
  for i:=1 to m do write(a[i,j],' ');
  writeln;
 end;
readln; end.

. Sumele obtinute de pe conturul unui patrat din matrice
var a:array[1..100,1..100] of integer;
      n,i,j,k,s:integer;
begin
write('n='); readln(n);
for i:=1 to m do
for j:=1 to n do
 begin write('a[',i,',',j,']='); readln(a[i,j]); end;
for k:=1 to n div 2 do
 begin
  s:=0;
  for j:=k to n-k+1 do s:=s+a[k,j];
  for i:=k+1 to n-k+1 do s:=s+a[i,n-k+1];
  for j:=n-k downto k do s:=s+a[n-k+1,j];
  for i:=n-k downto k+1 do s:=s+a[i,k];
  writeln('suma patratului ',k,' =',s);
 end;
if n mod 2<>0 then write(n div 2+1,' ',n div 2+1);
readln; end.

. Pozitiile calului pe tabla de sah
var i,c,x,y:integer;
begin
write('x='); readln(x);
write('y='); readln(y);
for i:=1 to 8 do
 case i of
  1:begin if (x-2>0) and (y+1>0) then writeln('(',x-2,',',y+1,')'); end;
  2:begin if (x-1>0) and (y+2>0) then writeln('(',x-1,',',y+2,')'); end;
  3:begin if (x+1>0) and (y+2>0) then writeln('(',x+1,',',y+2,')'); end;
  4:begin if (x+2>0) and (y+1>0) then writeln('(',x+2,',',y+1,')'); end;
  5:begin if (x+2>0) and (y-1>0) then writeln('(',x+2,',',y-1,')'); end;
  6:begin if (x+1>0) and (y-2>0) then writeln('(',x+1,',',y-2,')'); end;
  7:begin if (x-1>0) and (y-2>0) then writeln('(',x-1,',',y-2,')'); end;
  8:begin if (x-2>0) and (y-1>0) then writeln('(',x-2,',',y-1,')'); end;
 end;
readln; end.

. Transformarea literelor mari in litere mici
var s:string;
      i:integer;
begin
write('s='); readln(s);
for i:=1 to length(s) do
 if (s[i]>='A') and (s[i]<='Z') then
  s[i]:=char(ord(s[i])+32);
write(s);
readln; end.

. Verificare daca doua cuvinte rimeaza
var s,t,ss,tt:string;
      i,p:integer;
begin
write('s='); readln(s);
write('t='); readln(t);
write('p='); readln(p);
ss:=''; tt:='';
for i:=1 to p do
 begin
  ss:=ss+s[length(s)];
  delete(s,length(s),1);
 end;
for i:=1 to p do
 begin
  tt:=tt+length(t);
  delete(t,length(t),1);
 end;
if ss=tt then write('Cele doua cuvinte rimeaza')
           else write('Cele doua cuvinte nu rimeaza');
readln; end.

. Frecventa valorilor in vector (de cate ori apare fiecare valoare)
type sir=record
        nr,fr:integer;
       end;
var a:array[1..100,1..100] of integer;
       v:array[1..100] of sir;
       n,m,i,j,p,x,l,k,na:integer;
       g:boolean;
begin
write('n=');readln(n);
for i:=1 to n do
for j:=1 to m do
  begin write('a[',i,',',j,']='); readln(a[i,j]); end;
p:=0;
for i:=1 to n do
for j:=1 to m do
 begin
  g:=false;
  for x:=1 to p do if a[i,j]=v[x].nr then g:=true;
  if not g then begin
             na:=0;
             for k:=1 to n do
             for l:=1 to m do
              if a[i,j]=a[k,l] then na:=na+1;
             p:=p+1; v[p].nr:=a[i,j]; v[p].fr:=na;
            end;
  end;
for i:=1 to n do
for j:=1 to n do
 begin
  g:=false;
  for x:=1 to p do if a[i,j]=v[x].nr then
   begin g:=true; v[x].fr:=v[x].fr+1; end;
  if not g then begin p:=p+1; v[p].nr:=a[i,j]; v[p].fr:=1; end;
end;
for i:=1 to p do writeln(v[i].nr,' - ',v[i].fr);
readln; end.

. Generarea patratului magic: patratul in care adunand elementele de pe orice diagonala sau latura obtinem mereu aceeasi suma (numarul de linii si coloane n este impar)
var a:array[1..100,1..100] of integer;
       n,x,y,k,i,j:integer;
begin
write('n=');readln(n);
x:=n div 2+1; y:=n; a[x,y]:=1; k:=1;
repeat
 if y+1>n then begin x:=x+1; y:=1; end else
 if x+1>n then begin x:=1; y:=y+1; end else
  begin x:=x+1; y:=y+1 end;
 k:=k+1; a[x,y]:=k;
 if k mod n=0 then begin y:=y-1; k:=k+1; a[x,y]:=k; end;
until k=sqr(n);
for i:=1 to n do
  begin

   for j:=1 to n do write(a[i,j],' ');
   writeln;
  end;
readln;end.

. Numarul de cifre al unui numar
var n,nr:integer;
begin
write('n='); readln(n);
while n<>0 do
 begin
  nr:=nr+1;
  n:=n div 10;
 end;
write(n,' are ',nr,' cifre');
readln; end.

. Maximul dintr-un sir (cea mai mare valoare)
var n,max:integer;
begin
write('n='); readln(n);
max:=-maxint;
while n<>0 do
 begin
  if n>max then max:=n;
  write('n='); readln(n);
 end;
write('maximul: ',max);
readln; end.

. Minimul dintr-un sir (cea mai mica valoare)
var n,min:integer;
begin
write('n='); readln(n);
min:=maxint;
while n<>0 do
 begin
  if n<min then min:=n;
  write('n='); readln(n);
 end;
write('minimul: ',min);
readln; end.

. Suma cifrelor unui numar
var n,s,x:integer;
begin
write('n='); readln(n);
x:=n;
while n<>0 do
 begin
  s:=s+n mod 10;
  n:=n div 10;
 end;
write('suma cifrelor numarului ',x,' este: ',s);
readln; end.


Document Info


Accesari: 8317
Apreciat: hand-up

Comenteaza documentul:

Nu esti inregistrat
Trebuie sa fii utilizator inregistrat pentru a putea comenta


Creaza cont nou

A fost util?

Daca documentul a fost util si crezi ca merita
sa adaugi un link catre el la tine in site


in pagina web a site-ului tau.




eCoduri.com - coduri postale, contabile, CAEN sau bancare

Politica de confidentialitate | Termenii si conditii de utilizare




Copyright © Contact (SCRIGROUP Int. 2024 )