b为什么自测可以过,提交不对

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    long long n, x, totalCoins;
    
    cin >> n >> x;
    totalCoins = x;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    sort(a.begin(), a.end());

    long long count = 0;
    for (int i = 0; i < n; ++i) {
        if (x >= a[i]) {
            count++;
        } else {
            break;
        }
    }

    if (count == 0) {
        cout << count << " " << x << endl;
    } else {
        cout << count << " " << totalCoins - x << endl;
    }

    return 0;
}

}

全部评论
30行totalCoins - x换成x-a[i-1] 在for外定义i,就能过了
1
送花
回复
分享
发布于 03-17 22:24 河北
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { long long n, x, totalCoins; cin >> n >> x; totalCoins = x; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a.begin(), a.end()); long long count = 0; int i; for ( i = 0; i < n; ++i) { if (x >= a[i]) { count++; } else { break; } } if (count == 0) { cout << count << " " << x << endl; } else { cout << count << " " << x-a[i-1] << endl; } return 0; } </int></algorithm></vector></iostream>
1
送花
回复
分享
发布于 03-18 18:49 河北
滴滴
校招火热招聘中
官网直投
你这count不为0时剩余钱必输出0啊, 那万一我真剩下一两块呢
点赞
送花
回复
分享
发布于 03-18 13:29 广东
昨天没打上去代码
点赞
送花
回复
分享
发布于 03-18 18:50 河北

相关推荐

头像
05-12 09:14
点赞 评论 收藏
转发
2 收藏 评论
分享
牛客网
牛客企业服务