为什么我用for循环不对呀?求教
查看原帖
为什么我用for循环不对呀?求教
1461843
faithz楼主2025/1/3 17:23
#include <bits/stdc++.h>
using namespace std;

typedef long long LL ;
typedef pair<int,int>PII;
const int N = 1e6 + 5;
int a[N];

int main()
{
    cin.tie(0),cout.tie(0);
    ios::sync_with_stdio(0);
    int w,n;
    cin >> w >> n;
    for(int i = 0;i < n;i++)cin >> a[i];

    sort(a,a + n);

    int res = 0;
    for(int i = 0,j = n - 1;i <= j;)
    {
        int t = i;
        if(a[i] + a[j] <= w)
        {
            i ++;
            j --;
            res ++;
        }else 
        {
            res ++;
            j --;
        }
    }
    if(n % 2 == 1)res ++;
    cout << res << endl;
    return 0;
}
2025/1/3 17:23
加载中...