提交时间:2024-04-06 14:51:35

运行 ID: 141920

#include<iostream> using namespace std; const int N = 40; int s, n, a[N], cnt = 0; bool res[N], flag = false; void f(int x){ if(s == cnt){ for(int i = 0; i < n; i++){ if(res[i] == true) cout << a[i] << endl; } flag = true; return; } if(x == -1){ return; } cnt += a[x]; res[x] = true; f(x - 1); cnt -= a[x]; if(flag) return; res[x] = false; f(x - 1); if(flag) return; } int main(){ cin >> s >> n; for(int i = 0; i < n; i++){ cin >> a[i]; } f(n-1); return 0; }