-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
72 lines (61 loc) · 1.35 KB
/
Copy pathmain.cpp
File metadata and controls
72 lines (61 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
int i, utilizat[100], t[100], s, n, nr[100], k;
int valid(int sc) {
if (sc < 0)
return 0;
return 1;
}
int solutie(int sc) {
if (sc == 0)
return 1;
return 0;
}
void initial(int &sc) {
sc = s;
}
void bck(int &sc) {
initial(sc);
for (i = 1; i <= n; i++) {
if (sc / t[i] != 0) {
if (nr[i] >= sc / t[i]) {
if (valid(sc) == 1) {
nr[i] = nr[i] - sc / t[i];
utilizat[i] = sc / t[i];
sc = sc - sc / t[i] * t[i];
if (solutie(sc) == 1)
tipar();
else
bck(sc);
}
}
}
}
}
void tipar () {
k++;
cout << "\n";
cout << "Solutia numarul " << k << " este";
s = 0;
for (i = 1; i <= n; i++) {
cout << "\n";
cout << t[i] << " * " << utilizat[i] << " = " << t[i] * utilizat[i] << ", suma = " << (s += t[i] * utilizat[i]);
}
cout << "\n";
}
int main() {
FILE *f;
f = fopen("date.in", "r");
fscanf(f, "%d", &n);
for (i = 1; i <= n; i++) {
fscanf(f, "%d", &t[i]);
fscanf(f, "%d", &nr[i]);
}
fscanf(f, "%d", &s);
bck(s);
fclose(f);
return 0;
}