赞
踩
版本1:
- #include<bits/stdc++.h>
- using namespace std;
- #define int long long
- const int N=40+10;
- int a[N],n,l,r,cnt;
- void dfs(int x,int sum){
-
- if(sum>r){
- return;
- }
- if(sum>=l&&sum<=r){
- cnt++;
- }
- for(int i=x+1;i<=n;i++){
- dfs(i,sum+a[i]);
- }
- }
- void solve() {
- cin>>n>>l>>r;
- for(int i=1;i<=n;i++){
- cin>>a[i];
- }
- dfs(0,0);
- cout<<cnt<<"\n";
- }
- signed main() {
-
- ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
- int t=1;
- //cin>>t;
- while(t--) {
- solve();
- }
- }

版本2:
- #include<bits/stdc++.h>
- using namespace std;
- #define int long long
- const int N=40+10;
- int n,l,r,a[N],pre[N],cnt;
- void dfs(int x,int sum) {
- if(sum>r) {
- return;
- }
- if(x==n) { //结束
- if(sum>=l&&sum<=r) {
- cnt++;
- }
- return;
- }
- dfs(x+1,sum+a[x+1]);//选
- dfs(x+1,sum);//不选
- }
- void solve() {
- cin>>n>>l>>r;
- for(int i=1; i<=n; i++) {
- cin>>a[i];
- }
- dfs(0,0);
- cout<<cnt<<"\n";
- }
- signed main() {
-
- ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
- int t=1;
- //cin>>t;
- while(t--) {
- solve();
- }
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。