向阳菌:) 2022-01-15 10:43 采纳率: 63.2%
浏览 73
已结题

Linux运行C++程序不输出结果

问题遇到的现象和发生背景

Linux运行C++程序不输出结果,但是在网页的C++在线工具上却可以输出结果0,这是怎么回事?

问题相关代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#define x first
#define y second
using namespace std;
const int maxn = 1e5+10;
int ans=0;
typedef pair<int, int> PII;
bool cmp(PII a,PII b){
    return a.x<b.x;
}
  unordered_map<int,int>mp_up,mp_down;
int main(){
    int n;
    cin>>n;
    PII up[maxn],down[maxn];
    for (int i = 0; i < n; i ++ ){
         int a,b;
         cin>>a>>b;
         PII t;
         mp_up[a]=b;
         mp_down[b]=a;
         up[i]={a,i};
         down[i]={b,i};
         
    }
    sort(up,up+n,cmp),  sort(down,down+n,cmp);;
    int ans=0;
    int max_up =-0x3f3f3f3f;int max_down =-0x3f3f3f3f;
    for (int i = 0; i < n; i ++ ){
        if(up[i].y!=down[i].y){
            max_down=max(max_down,mp_up[up[i].x]);
            max_up=max(max_up,mp_down[down[i].x]);
            
        }
        if(up[i].y==down[i].y&&(down[i].x>max_down&&up[i].x>max_up)){
            ans++;
            
        }
    }
    cout << ans <<endl;
    
}

运行结果及报错内容

img

img

  • 写回答

2条回答 默认 最新

  • 墨色染夕 若水三千 2022-01-15 19:51
    关注
    
    //const int maxn = 1e5 + 10;warning C4244: “初始化”: 从“double”转换到“int”,可能丢失数据
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <unordered_map>
    #define x first
    #define y second
    using namespace std;
    const int maxn = 1e5 + 10;
    int ans = 0;
    typedef pair<int, int> PII;
    bool cmp(PII a, PII b) {
        return a.x<b.x;
    }
    unordered_map<int, int>mp_up, mp_down;
    int main() {
        int n;
        cin >> n;
        PII up[maxn], down[maxn];
        for (int i = 0; i < n; i++) {
            int a, b;
            cin >> a >> b;
            PII t;
            mp_up[a] = b;
            mp_down[b] = a;
            up[i] = { a,i };
            down[i] = { b,i };
    
        }
        sort(up, up + n, cmp); sort(down, down + n, cmp);
        int ans = 0;
        int max_up = -0x3f3f3f3f; int max_down = -0x3f3f3f3f;
        for (int i = 0; i < n; i++) {
            if (up[i].y != down[i].y) {
                max_down = max(max_down, mp_up[up[i].x]);
                max_up = max(max_up, mp_down[down[i].x]);
    
            }
            if ((up[i].y == down[i].y) && (down[i].x>max_down&&up[i].x&&max_down&&up[i].x>max_up)) {
                ans++;
    
            }
        }
        cout << ans << endl;
    
    }
    
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月25日
  • 修改了问题 1月15日
  • 修改了问题 1月15日
  • 创建了问题 1月15日