You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在CodeChef运行C++代码时触发SIGFPE运行时错误,如何解决该问题?

Fixing SIGFPE Error in Your C++ Code

Hey there! Let's break down why you're getting that SIGFPE error and how to fix it.

First, SIGFPE in integer code almost always means division by zero. Looking at your code, the culprit is this line:

if (i % myvector[j].second != 0)

If myvector[j].second (which comes from your input y[i]) is zero, you're trying to compute i % 0—which is undefined and triggers the SIGFPE error.

Key Fixes & Improvements

  1. Handle Division by Zero (y=0 Case)
    When y is zero, there's no valid integer i divisible by zero. So we treat this as a special case where all positions should be set to x (since every position would satisfy the "not divisible by y" condition if it were valid). We add a check to avoid the modulo operation entirely when y is zero.

  2. Replace Variable-Length Arrays (VLAs) with Vectors
    VLAs like int arr[n+1]; aren't standard C++ and can cause unexpected behavior in some compilers. Using vector<int> is safer, more flexible, and initializes values to zero by default (so we don't need memset anymore).

  3. Clean Up Loop Structure
    Your nested do-while loops had a redundant variable declaration (int i=1; inside the inner loop) that shadowed the outer variable. Switching to for loops makes the code more readable and avoids this issue.

  4. Sum Only Relevant Positions
    Your original code summed the entire arr (including index 0, which is unused). We now sum only positions 1 to n to avoid any accidental issues.

Corrected Code

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

int main() {
    int T;
    cin >> T;
    while (T--) {
        int n, m;
        cin >> n >> m;
        vector<int> arr(n + 1, 0); // Initialize array with zeros
        vector<int> x(m), y(m);
        
        for (int i = 0; i < m; ++i) {
            cin >> x[i] >> y[i];
        }
        
        vector<pair<int, int>> constraints;
        for (int i = 0; i < m; ++i) {
            constraints.emplace_back(x[i], y[i]);
        }
        
        sort(constraints.begin(), constraints.end()); // Sort by x ascending, then y
        
        for (const auto& p : constraints) {
            int x_val = p.first;
            int y_val = p.second;
            
            if (y_val == 0) {
                // Special case: set all positions to x_val
                fill(arr.begin() + 1, arr.end(), x_val);
            } else {
                for (int k = 1; k <= n; ++k) {
                    if (k % y_val != 0) {
                        arr[k] = x_val;
                    }
                }
            }
        }
        
        int sum = 0;
        for (int k = 1; k <= n; ++k) {
            sum += arr[k];
        }
        cout << sum << endl;
    }
    return 0;
}

Explanation of Changes

  • y=0 Handling: We use fill to set all positions from 1 to n to x_val when y is zero, avoiding any division operation.
  • Vectors: vector<int> arr(n+1, 0) creates an array of size n+1 initialized to zero, no need for memset.
  • Readable Loops: For loops make the flow clearer, and we use range-based for loops to iterate over constraints.
  • Accurate Sum: We only sum positions 1 to n since arr[0] is unused and shouldn't contribute to the result.

This code should resolve the SIGFPE error and work correctly with your test cases.

内容的提问来源于stack exchange,提问作者Kuntal Manna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 05:02:34