Skip to content

Instantly share code, notes, and snippets.

@jongha
Created June 10, 2019 15:57
Show Gist options
  • Save jongha/a4159f5bfeb625104877a6d6dd6b20c9 to your computer and use it in GitHub Desktop.
Save jongha/a4159f5bfeb625104877a6d6dd6b20c9 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string.h>
#define MAX 1000
int m[MAX];
using namespace std;
list<int> lst;
vector< vector<int> > result;
int t = 0, n = 0;
bool found = false;
void find(int pos, int sub_t) {
if(t == sub_t) {
for(int i=0; i<result.size(); i++) {
if(result[i].size() != lst.size()) continue;
bool eq = true;
int idx = 0;
for(list<int>::reverse_iterator p = lst.rbegin(); eq && p != lst.rend(); p++) {
eq = result[i][idx++] == m[*p];
}
if(eq) return;
}
vector<int> v;
for(list<int>::reverse_iterator p = lst.rbegin(); p != lst.rend(); p++) {
if(p != lst.rbegin()) printf("+");
printf("%d", m[*p]);
v.push_back(m[*p]);
found = true;
}
result.push_back(v);
printf("\n");
return;
}
for(int i=0; i<n; i++) {
if(i > pos) {
lst.push_front(i);
find(i, m[i] + sub_t);
lst.pop_front();
}
}
}
int main(void) {
scanf("%d %d", &t, &n);
while (n != 0) {
for(int i=0; i<n; i++) {
scanf("%d", &m[i]);
}
lst.clear();
result.clear();
found = false;
printf("Sums of %d:\n", t);
find(-1, 0);
if(!found) printf("NONE\n", t);
scanf("%d %d", &t, &n);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment