arc_suvr @ 2022-04-27 08:18:46
#include <bits/stdc++.h>
using namespace std;
int n, l, r, a[45], ans = 0;
void dfs(int x, int res) {
if (res >= l && res <= r) ans++;
for (int i = x + 1; i <= n; i++) {
int nowres = res + a[i];
if (nowres > r) return ;
dfs(i, nowres);
}
return ;
}
int main() {
cin >> n >> l >> r;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
dfs(i, a[i]);
}
cout << ans << endl;
}
by MujicaSaki @ 2022-04-27 08:22:07
int nowres = res + a[i];
改成
int nowres = res * a[i];
我猜的(
by MujicaSaki @ 2022-04-27 08:22:17
@终生蓝狐
by arc_suvr @ 2022-04-27 08:36:32
@QAQ__QWQ thanks 但好像不是这个问题,题目中没有说是乘积
by MujicaSaki @ 2022-04-27 08:41:49
@终生蓝狐
#include <bits/stdc++.h>
using namespace std;
int n,l,r,ans;
int a[55],w[55];
void dfs(int p){
int sum=0;
for(int i=1;i<=p-1;i++) sum+=a[i]*w[i];
if(sum>r) return;
if(p==n+1) {
if(l<=sum&&sum<=r) ans++;
return;
}
for(int i=0;i<=1;i++) {
a[p]=i;
dfs(p+1);
}
}
int main() {
cin>>n>>l>>r;
for(int i=1;i<=n;i++) cin >> w[i];
dfs(1);
cout<<ans<<endl;
}
我的代码你看着改吧。。。
by 编码落寞 @ 2022-04-27 09:00:09
@终生蓝狐
建议输入之后排个序,不知道给的数据是不是乱序的。例如:
5 80 82
20 10 80 60 3
这组数据就过不去
by arc_suvr @ 2022-04-27 09:32:39
@QAQ__QWQ @编码落寞 谢谢
by arc_suvr @ 2022-04-27 09:34:26
@编码落寞 我觉得和排序没有关系吧,因为我的代码是0分
by 编码落寞 @ 2022-04-27 09:40:15
@终生蓝狐
排序过后确实只对了一个点
by 编码落寞 @ 2022-04-27 09:49:44
@终生蓝狐
加个排序, 并把
if (nowres > r) return ;
这部分去掉。5A5T
by 编码落寞 @ 2022-04-27 09:56:46
@终生蓝狐
#include <bits/stdc++.h>
using namespace std;
int n, l, r, a[45], ans = 0;
void dfs(int x, int res) {
if (res >= l && res <= r)
{
ans++;//cout<<"1111::::"<<x<<endl;
}
if(res > r)
return;
for (int i = x + 1; i <= n; i++) {
int nowres = res + a[i];
dfs(i, nowres);
}
return ;
}
int main() {
cin >> n >> l >> r;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a,a+n);
for (int i = 1; i <= n; i++) {
dfs(i, a[i]);
}
cout << ans << endl;
}
你的if判断导致了有些情况排除了。最好把if放外面