提交时间:2024-04-06 15:13:34

运行 ID: 141958

# include <bits/stdc++.h> using namespace std ; int w [33] , s , n ; int main ( ) { cin >> s >> n ; for ( int i = 1 ; i <= n ; i ++ ) { cin >> w [i] ; } for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int sum = 0 ; memset ( u , 0 , sizeof ( u ) ) ; for ( int j = 1 ; j <= n ; j ++ ) { if ( 1 << i & num ) { sum += w [i] ; u [i] = 1 ; } } if ( sum == s ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( u [j] ) { cout << w [j] << endl ; } } return 0 ; } } cout << "Failed!" << endl ; return 0 ; }