-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
111 lines (92 loc) · 2.68 KB
/
Copy pathmain.cpp
File metadata and controls
111 lines (92 loc) · 2.68 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include "Node.h"
using namespace std;
void algorithm(Node* root, vector<int> list, int total, const int sum);
void printCombination(Node*);
bool found = false; //Global Variable to keep track if the algorithm found a combination or not
int main()
{
//To store the list of candidates
vector<int> list;
//For reading files
ifstream infile;
infile.open("numberselect.txt");
string numbers;
getline(infile, numbers); //Skip first line
getline(infile, numbers);
stringstream num(numbers);
int value;
while (num >> value)
{
if (value <= 0)
{
cout << "ERROR encountered: Please enter a number > 0" << endl;
return 0;
}
else
{
list.push_back(value);
}
}
getline(infile, numbers); //Skip third line
getline(infile, numbers);
int sum = stoi(numbers);
if (sum <= 0)
{
cout << "ERROR encountered: Please enter a number > 0" << endl;
return 0;
}
int total = 0; //Partial total to maintain as algorithm goes further down
for (int i = 0; i < list.size(); i++)
{
Node* root = new Node; //Create root node for each number in the list
root->num = list[i];
root->parent = NULL;
total += root->num;
if (total == sum) //Base case: sum = one of the candidates
{
cout << "Combination Found: " << endl;
cout << root->num << endl;
}
algorithm(root, list, total, sum);
total = 0; //Reset the total
}
if (!found)
{
cout << "NO COMBINATION FOUND :(" << endl;
}
}
/*Recursive Algorithm to list all possible patterns that add to a certain sum */
void algorithm(Node* root, vector<int> list, int total, const int sum)
{
for (int i = 0; i < list.size(); i++)
{
Node* newNode = root->createChild(list[i]); //Create the child
total += newNode->num;
if (total == sum) //Found a combination
{
found = true;
cout << "Combination: " << endl;
printCombination(newNode);
cout << endl;
}
else if (total < sum) //Only go further if the total hasn't surpassed the actual total we want
{
algorithm(newNode, list, total, sum);
}
total -= newNode->num; //Subtract the amount added because new loop means new child
}
}
//Starting from the leaf node, prints out the combination.
void printCombination(Node* leaf)
{
if (leaf == NULL)
{
return;
}
cout << leaf->num << " ";
printCombination(leaf->parent);
}