原题链接
题意
来源:洛谷
思路
排序后,只加选在范围内的数,如果总和大于k就退出
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 150;
int a[N];
signed main()
{
int t; cin >> t;while (t -- ){
int n, l ,r, k;cin >> n >> l >> r >> k;for (int i = 1; i <= n; i ++ ){
cin >> a[i];}sort(a + 1, a + 1 + n);int sum = 0, ans = 0;for (int i = 1; i <= n; i ++ ){
if (a[i] >= l && a[i] <= r){
sum += a[i];if (sum > k) break;ans ++;}}cout << ans << endl;}return 0;
}