简体   繁体   English

C++:freopen(和 I/O)只是拒绝工作

[英]C++: freopen (and I/O) just refuses to work

Today I'm having issues with my code.今天我的代码有问题。 It appears that I can not get anything from input (whether file or stdin) as well as unable to print (whether from file or stdout).看来我无法从输入(无论是文件还是标准输入)中获得任何东西,也无法打印(无论是从文件还是标准输出)。 My code can have a lot of issues (well, this is code for a competitive programming problem, don't expect it to be good. It will be straight up horrendous and violate everything you know about C++).我的代码可能有很多问题(嗯,这是针对竞争性编程问题的代码,不要指望它会很好。这将是非常可怕的,并且违反了您对 C++ 的所有了解)。

#include <bits/stdc++.h>
#include <climits>
#include <vector>
using namespace std;
#define mp make_pair
#define endl "\n"
#define ll long long
#define ld long double 
const int nm = 1e3 + 1;
const int mod  = 1e9 + 7;
void fastio(string fi, string fo){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if (fi.length() > 0){
        freopen(fi.c_str(), "r", stdin);
    }
    if (fo.length() > 0){
        freopen(fo.c_str(), "w", stdout);
    }
}

int m, n;
int demsm(int k, int p){
    int c = 0, r = p;
    while (r <= k){
        c += int(k / r);
        r *= p;
    }
    return c;
}

vector<pair<int, int>> uoc(int x){
    vector<pair<int, int>> u;
    for (int i = 2; x != 1; i++){
        int c = 0;
        while (x % i == 0){
            c++;
            x /= i;
        }
        u.emplace_back(mp(i, c));
    }
    return u;
}

int main(){
    fastio("", "");
    cin >> n >> m;
    cout << n << " " << m << endl;
    auto snt = uoc(m);
    for (auto i : snt){
        cout << i.first << " " << i.second << endl;
    }
    int a = 1e9;
    for (auto i : snt){
        a = min(a, demsm(n, i.first) / i.second);
    }
    cout << a << endl;
}

This isn't an issue with freopen or I/O.这不是 freopen 或 I/O 的问题。 Here a = min(a, demsm(n, i.first) / i.second);这里a = min(a, demsm(n, i.first) / i.second); You are dividing by zero and this results a segmentation error.您正在除以零,这会导致分段错误。

The reason why the program isn't printing anything is because of the way how these lines work.程序不打印任何内容的原因是这些行的工作方式。

ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

You can read about them in this question .您可以在这个问题中了解它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM