Sunteți pe pagina 1din 3

S1

1 d)
2 a) 62255661
b) 1201 3452 560 78
c) #include <iostream>

using namespace std;

int main() {
int a=0, k=0, x;
do {
cin>>x;
while (x>99) {
x=x/10;
}
if (x>9) {
a=a*100+x;
k++;
}
} while (k!=4);
cout<<a;
return 0;
}
d) a<-0
k<-0
|-cât timp k<4 execută
| citeşte x
| |-cât timp x>99 execută
| | x<-[x/10]
| |-
| |-dacă x>9 atunci
| | a<-a*100+x
| | k<-k+1
| |-
|-
scrie a

S2
1 b)
2 d)
3 -
4 nr minim: 2
arcele: (2, 3) (4, 6)
5 #include <iostream>

using namespace std;

int main() {
int n, a[30][30];
cin>>n;
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
a[i][j]=(i+j)*(i+j);
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}

S3
1 d)
2 -6 -2 0 5 10 7
3 a) int UltimaCifra(int x, int y) {
int putere;
if (y==0) {
return 1;
}
putere=UltimaCifra(x, y/2);
if (y%2==0) {
return putere*putere%10;
}
else {
return x*putere*putere%10;
}
}
/*
int UltimaCifra(int x, int y) {
int putere=1;
x=x%10;
if (x==0) {
return 0;
}
while (y>0) {
if (y%2) {
putere=(putere*x)%10;
}
y=y/2;
x=(x*x)%10;
}
return putere;
}
*/
b) Soluția de mai sus calculeaza o singura data UltimaCifra(x, y/2) si o
stocheaza, împartind problema în subprobleme de dimensiunea y/2 și
apeleazand subproblemele recursiv, astfel avand o complexitate O(logn).
c) #include <iostream>
#include <fstream>

int UltimaCifra(int x, int y) {


int putere;
if (y==0) {
return 1;
}
putere=UltimaCifra(x, y/2);
if (y%2==0) {
return putere*putere%10;
}
else {
return x*putere*putere%10;
}
}

using namespace std;


int main() {
int x, y, n, s=0;
ifstream f("SIR.IN");
ofstream g("SIR.OUT");
f>>n;
while (f>>x>>y) {
s=(s+UltimaCifra(x, y))%10;
}
g<<s;
f.close();
g.close();
return 0;
}

S-ar putea să vă placă și