-
I. 1.
b) !(x < 3 || x > 7)
I. 2.
a) 2
b) a = 61 , b = 99
c)
CITESTE a , b
DACA a < b ATUNCI
INTERSCHIMBA a cu b
CAT TIMP b != 0 EXECUTA
c <- a
CAT TIMP c >= b EXECUTA
c <- c - b
a <- b
b <- c
RETURNEAZA a
d)
#include <iostream>
using namespace std;
int a, b, c;
int main(){
cin>>a>>b;
while(a < b){
int aux = a;
a = b;
b = aux;
}
while(b != 0){
c = a % b;
a = b;
b = c;
}
cout<<a;
return 0;
}
a. 36
II. 2.
d. 4
II. 3.
push(s3, pop(s1))
push(s3, pop(s1))
push(s3, pop(s1))
push(s2, pop(s3))
push(s2, pop(s3))
push(s2, pop(s3))
II. 4.
#include <iostream>
#include <cstring>
using namespace std;
char s[105];
int fv[105];
int main(){
cin>>s;
for(int i = 0; i < strlen(s); i++){
int k = (int)(s[i]) - (int)('a');
fv[k]++;
}
for(int i = 0; i < 26; i++)
if(fv[i] > 0){
cout<<(char)(i + (int)('a'))<<": "<<fv[i]<<"\n";
}
return 0;
}
II. 5.
#include <iostream>
using namespace std;
int n, m[105][105];
int main(){
cin>>n;
m[0][0] = 1;
for(int j = 1; j < n; j++)
m[0][j] = m[0][j - 1] + 1;
for(int i = 1; i < n; i++){
m[i][0] = m[i - 1][1];
for(int j = 1; j < n; j++){
m[i][j] = m[i][j - 1] + 1;
if(m[i][j] > n)
m[i][j] = 1;
}
}
for(int i = 0; i < n; i++, cout<<"\n")
for(int j = 0; j < n; j++)
cout<<m[i][j]<<" ";
return 0;
}
III. 1.
d. 120
III. 2.
F(11) = 2048
III. 3.
a)
O varianta valida pentru rezolvarea ecuatiei descrise se bazeaza pe metoda backtracking.
Din moment ce cunoastem numarul termenilor (n = 2) si valorile pe care le pot lua necunoscutele x0, x1
se afla in intervalul [inf, sup], putem atribui celor 2 necunoscute toate valorile posibile din interval.
In momentul in care suma a0 * x0 + a1 * x1 devine egala cu b, atunci am gasit o solutie valida pentru ecuatia data.
b)
int min(int n, int a[105], int b, int inf, int sup, int k){
int sum = 0;
for(int i = 0; i < n; i++)
if(a[i] < 0)
sum += a[i] * sup;
else
sum += a[i] * inf;
return sum;
}
int max(int n, int a[105], int b, int inf, int sup, int k){
int sum = 0;
for(int i = 0; i < n; i++)
if(a[i] < 0)
sum += a[i] * inf;
else
sum += a[i] * sup;
return sum
}
c)
void back(int n, int a[105], int inf, int sup, int sol[105], int k, bool & g){
for(int i = inf; i <= sup && g == false; i++){
sol[k] = i;
if(k == (n - 1)){
int sum = 0;
for(int j = 0; j < n; j++)
sum += a[j] * sol[j];
if(sum == b)
g = true;
}
else
back(n, a, inf, sup, sol, k, g)
}
}