-
I. 1.
D. !(a >= 9) && (a > 7)
I. 2.
a) F(2395) = 0
b) 1313
subprogram FRec(n)
DACA n = 0 ATUNCI
RETURNEAZA 1
ALTFEL
DACA ( (n % 2 == 0) SAU ( (n > 9) && (n % 10 == (n / 10) % 10 ) ) ) ATUNCI
RETURNEAZA 0
ALTFEL
RETURNEAZA FRec(n / 10)
d)
#include <iostream>
using namespace std;
int F(int n){
int r = 1, c = 0;
while(r != 0 && n != 0){
if(n % 2 == 0 || n % 10 == c){
r = 0;
}
else{
c = n % 10;
n = n / 10;
}
}
return r;
}
II. 1.
C. 292
II. 2.
A. 21
II. 3.
a)
0 -> 3 -> 1 -> 2
b)
O ordonare este posibila doar daca avem cel
putin 2 echipe astfel: Daca n = 2 atunci avem i
-> j sau j -> i
Daca n > 2 atunci pentru o noua echipa x avem:
I) x -> i atunci ordonarea va fi x -> i -> j
II) j -> x atunci ordonarea va fi i -> j -> x
III) i -> x si x -> j atunci ordonarea va fi i
-> x -> j
Astfel pentru orice n >= 2 putem garanta
existenta unei solutii.
c)
void ordonare(int n, int a[105][105], int s[105]){
int i, j, sz = 0;
for(i = 0; i < n && sz != 1; i++)
for(j = 0; j < n && sz != 1; j++)
if(a[i][j])
s[sz] = i, s[++sz] = j;
for(int x = 0; x < n; x++)
if(x != i && x != j){
if(a[x][s[0]]){
sz++;
for(int k = sz; k > 0; k--)
s[k] = s[k - 1];
s[0] = x;
}
else
if(a[s[sz]][x])
s[++sz] = x;
else{
for(int k = 0; k < sz; k++)
if(a[k][x] && a[x][k + 1]){
sz++;
for(int y = sz; y > (k + 1); y--)
s[y] = s[y - 1];
s[k + 1] = y;
}
}
}
}
II. 4.
a)
0 1 1 1 1 1 1
1 0 1 0 0 0 1
1 1 0 1 0 0 0
1 0 1 0 1 0 0
1 0 0 1 0 1 0
1 0 0 0 1 0 1
1 1 0 0 0 1 0
b)
void retea(int n, int m[105][105]){
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
m[i][j] = 0;
for(int i = 1; i < n; i++){
m[0][i] = m[i][0] = 1;
m[i][i + 1] = m[i + 1][i] = 1;
}
m[1][n - 1] = m[n - 1][1] = 1;
}
III. 1.
rac(2, 0) = -3
III. 2.
C. 3
III. 3.
a)
int verifica (int A[105][105], int N, int M){
for(int i = 0; i < (N - 2); i++)
for(int j = 0; j < (M - 2); j++){
if(A[i][j] < A[i + 1][j] && A[i][j] < A[i][j + 1] && A[i + 1][j + 1] < A[i + 1][j] && A[i + 1][j + 1] < A[i][j + 1])
return 0;
if(A[i + 1][j] < A[i][j] && A[i + 1][j] < A[i + 1][j + 1] && A[i][j + 1] < A[i][j] && A[i][j + 1] < A[i + 1][j + 1])
return 0;
return 1;
}
}
b)
int complet(int A[105][105], int N, int M){
int sum = 0;
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
sum += A[i][j];
return sum;
}
c)
int optim(int A[105][105], int N, int M, int K){
int X = -1;
for(int i = 0; i <= N - K; i++)
for(int j = 0; j <= M - K; j++){
int di[] = {-1, 0, 1, 0};
int dj[] = {0, 1, 0, -1};
int l, c, m = -1;
int a[105][105];
for(int x = 0; x < N; x++)
for(int y = 0; y < M; y++)
a[x][y] = A[x][y];
for(int x = i; x <= i + K - 1; x++)
for(int y = j; y <= j + K - 1; y++)
if(m == -1)
m = A[x][y], l = x, c = y;
else
if(m > A[x][y])
m = A[x][y], l = x, c = y;
struct P
{
int lin, col;
}q[N * M + 5];
int st = 1, dr = 1, cnt = 1;
q[st].lin = l, q[st].col = c; a[l][c] = -2;
while(st <= dr){
int x = q[st].lin, y = q[st].col;
st++;
for(int k = 0; k < 4; k++){
int iv = x + di[k], jv = y + dj[k];
if(iv >= 0 && iv < N && jv >= 0 && jv < M && a[iv][jv] >= m){
cnt += a[iv][jv] - m + 1;
a[iv][jv] = -2;
q[++dr].lin = iv;
q[dr].col = jv;
}
}
}
if(X == -1)
X = cnt;
else
if(X > cnt)
X = cnt;
}
return X;
}