【问题标题】:Runtime Error on CodeChef: SIGSEGV - CodeChefCodeChef 上的运行时错误:SIGSEGV - CodeChef
【发布时间】:2020-04-27 04:45:28
【问题描述】:

我正在解决这个问题:https://www.codechef.com/LTIME83B/problems/FFL

但是在这我得到RE (SIGSEGV)。我不明白,为什么我会得到这个?

我认为我没有超出数组的限制。

那为什么会出现这个错误呢?

我的代码:

#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#define ll long long
using namespace std;


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll t;
    cin >> t;
    while (t--)
    {
        ll n, s;
        cin >> n >> s;
        s = 100 - s;
        ll i, a[n], k;
        vector<ll> g1;
        vector<ll> g2;
        for (i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        for (i = 0; i < n; i++)
        {
            cin >> k;
            if (k == 0)
            {
                g1.push_back(a[i]);
            }
            if (k == 1)
            {
                g2.push_back(a[i]);
            }
        }
        ll o = *min_element(g1.begin(), g1.end());
        ll p = *min_element(g2.begin(), g2.end());
        if (s >= o + p)
        {
            cout << "yes" << endl;
        }
        else
        {
            cout << "no"<<endl;
        }
    }
    return 0;
}

【问题讨论】:

  • long long a[n] 在 C++ 中无效。 n 必须是编译时间常数。
  • @S.M.我猜他使用cin 获取n 的值。
  • @sbak 如果疑问已解决,则接受答案,以便将问题标记为已解决。

标签: c++ algorithm


【解决方案1】:

您没有处理所有可用玩家都是防守方或进攻方的情况。

#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#define ll long long
using namespace std;


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll t;
    cin >> t;
    while (t--)
    {
        ll n, s;
        cin >> n >> s;
        s = 100 - s;
        ll i, a[n], k;
        vector<ll> g1;
        vector<ll> g2;
        for (i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        for (i = 0; i < n; i++)
        {
            cin >> k;
            if (k == 0)
            {
                g1.push_back(a[i]);
            }
            if (k == 1)
            {
                g2.push_back(a[i]);
            }
        }
        if(g1.size() == 0 || g2.size() == 0){
            cout<<"no\n";
            continue;
        }
        ll o = *min_element(g1.begin(), g1.end());
        ll p = *min_element(g2.begin(), g2.end());
        if (s >= o + p)
        {
            cout << "yes" << endl;
        }
        else
        {
            cout << "no"<<endl;
        }
    }
    return 0;
}

尝试提交此代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多