I. 1.
b) (a / b == 9) && (a % b > 0)
I. 2.
a) 3
b) a = 16
c)
CITESTE a
b <- 4 * a
min <- -1
x <- 1
CAT TIMP x <= b EXECUTA
y <- 1
CAT TIMP y <= b / x EXECUTA
aux <- | x + y - a | + | x * y - b |
DACA min = -1 SAU aux < min ATUNCI
min = aux
y <- y + 1
x <- x + 1
RETURNEAZA min
d)
#include <iostream>
#include <cmath>
using namespace std;
int a, b, minim = -1, aux;
int main(){
cin>>a;
b = 4 * a;
for(int x = 1; x <= b; x ++)
for(int y = 1; y <= b / x; y ++){
aux = abs(x + y - a) + abs(x * y - b);
if(minim == -1 || aux < minim)
minim = aux;
}
cout<<minim;
}
II. 1.
c. 71
II. 2.
d) nicio varianta
II. 3.
#include <iostream>
#include <cstdlib>
using namespace std;
int n, m, k, sum, cnt, row1[105], col1[105], val1[105], row2[105], col2[105], val2[105];
int main(){
cin>>n>>m>>k;
for(int i = 0; i < n && cnt < k; i ++)
for(int j = 0; j < n && cnt < k; j ++){
row1[cnt] = row2[cnt] = i;
col1[cnt] = col2[cnt] = j;
val1[cnt] = rand() % 100001 + 1;
val2[cnt] = rand() % 100001 + 1;
cnt++;
sum += val1[cnt] + val2[cnt];
}
cnt = 0;
cout<<"M1 "<<"\n";
for(int i = 0; i < n; i ++, cout<<"\n")
for(int j = 0; j < n; j ++){
if(cnt < k){
cout<<val1[cnt]<<" ";
cnt++;
}
else
cout<<0<<" ";
}
cnt = 0;
cout<<"M2 "<<"\n";
for(int i = 0; i < n; i ++, cout<<"\n")
for(int j = 0; j < n; j ++){
if(cnt < k){
cout<<val2[cnt]<<" ";
cnt++;
}
else
cout<<0<<" ";
}
cout<<"Suma M1 + M2 = "<<sum;
return 0;
}
II. 4.
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
char c[105], a[105][105], s[105];
int n, k;
int main(){
cin>>c;
n = sqrt( strlen(c) );
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++)
a[i][j] = c[k ++];
k = 0;
for(int i = 0; i < n; i ++)
s[k++] = a[i][n - i - 1];
for(int i = 0; i < n; i ++)
s[k ++] = a[i][i];
s[k] = '\0';
cout<<s;
return 0;
}
III. 1.
Nicio varianta
III. 2.
F(2, 1) = 5
III. 3.
a)
G R E U J
T E S T O
I N F O C
b)
c)
O solutie valida se bazeaza pe metoda backtracking.
Pentru fiecare cuvant din multimea data , incercam sa il plasam
pe orizontala, respectiv pe verticala, daca gasim o plasare
atunci ne putem muta la urmatorul cuvant din lista.
Daca gasim o pozitie valida pentru urmatorul cuvant pe orizontala
respectiv pe verticala ne putem muta iarasi la urmatorul cuvant ...
si asa mai departe
In caz contrar, trebuie sa revenim la cuvantul precedent
si sa incercam o alta plasare
In cazul in care nu am gasit nicio plasare valida pentru cuvantul precedent
si cel curent inseamna ca nu avem nicio solutie.
#include <iostream>
using namespace std;
void checkVertical(int x, int y, char temp[105][105], char currentWord[]){
int n = strlen(currentWord);
for(int i = 0; i < n; i++){
if(temp[x + i][y] == '-' || temp[x + i][y] == currentWord[i])
temp[x + i][y] = currentWord[i];
else
temp[0][0] = '@';
}
}
void checkHorizontal(int x, int y, char temp[105][105], char currentWord[]){
int n = strlen(currentWord);
for(int i = 0; i < n; i++){
if(temp[x][y + i] == '-' || temp[x][y + i] == currentWord[i])
temp[x][y + i] = currentWord[i];
else
temp[0][0] = '@';
}
}
void back(char words[105][105], int sz, int n, int m, char matrix[105][105], int index, bool & g, char sol[105][105]){
if(index < sz && g == false){
int maxLen = n - strlen(words[index]);
for (int i = 0; i < m; i++) {
for (int j = 0; j <= maxLen; j++) {
char temp[105][105];
for(int x = 0; x < n; x++)
for(int y = 0; y < m; y++)
temp[x][y] = matrix[x][y];
checkVertical(j, i, temp, words[index]);
if (temp[0][0] != '@') {
back(words, sz, n, m, temp, index + 1, g, sol);
}
}
}
maxLen = m - strlen(words[index]);
for (int i = 0; i < n; i++) {
for (int j = 0; j <= maxLen; j++) {
char temp[105][105];
for(int x = 0; x < n; x++)
for(int y = 0; y < m; y++)
temp[x][y] = matrix[x][y];
checkHorizontal(i, j, temp, words[index]);
if (temp[0][0] != '@') {
back(words, sz, n, m, temp, index + 1, g, sol);
}
}
}
}
else
if(index == sz && g == false){
g = true;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
sol[i][j] = matrix[i][j];
}
}
void solvePuzzle(char words[105][105], int sz, int n, int m, char sol[105][105]){
char matrix[105][105];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
sol[i][j] = matrix[i][j] = '-';
bool g = false;
back(words, sz, n, m, matrix, 0, g, sol);
}
int main(){
char words[105][105] = {"INFO", "GREU", "TEST", "REN", "JOC", "FOC"};
char matrix[105][105];
int n = 3;
int m = 5;
solvePuzzle(words, 6, n, m, matrix);
for(int i = 0; i < n; i++, cout<<"\n")
for(int j = 0; j < m; j++)
cout<<matrix[i][j]<<" ";
}