Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 这段代码有浮点错误,有人能帮我吗_C++_Floating Point - Fatal编程技术网

C++ 这段代码有浮点错误,有人能帮我吗

C++ 这段代码有浮点错误,有人能帮我吗,c++,floating-point,C++,Floating Point,我面临着这个中国人的问题 我正在使用input.txt文件作为输入 并尝试生成output.txt文件,但显示浮点错误。当我在运行一些特定的输入时,它是工作的,但是对于许多测试用例来说它是不工作的 #include <bits/stdc++.h> #include <fstream> #include <iostream> using namespace std; int ModInverse(int a, int m) { a = a % m;

我面临着这个中国人的问题 我正在使用input.txt文件作为输入 并尝试生成output.txt文件,但显示浮点错误。当我在运行一些特定的输入时,它是工作的,但是对于许多测试用例来说它是不工作的

#include <bits/stdc++.h>
#include <fstream>
#include <iostream>

using namespace std;

int ModInverse(int a, int m)
{
    a = a % m;
    for(int x = 1; x < m; x++)
        if(((a * x) % m) == 1) return x;
}

int findMinX(int num[], int rem[], int k) {
    int prod = 1;

    for(int i = 1; i <= k; i++) prod *= num[i];

    int result = 0;
    for(int j = 1; j <= k; j++) {
        int y = prod / num[j];

        result = result + rem[j] * ModInverse(y, num[j]) * y;
    }

    return result % prod;
}
int main() {
    ifstream infile;
    infile.open("input.txt");

    int n;
    int num[100];
    int rem[100];

    infile >> n;
    for(int i = 0; i < n; i++) infile >> num[i];

    for(int i = 0; i < n; i++) infile >> rem[i];

    infile.close();

    int k = sizeof(num) / sizeof(num[0]);

    ofstream myfile;
    myfile.open("output.txt");
    myfile << findMinX(num, rem, k);
    myfile.close();

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int ModInverse(int a,int m)
{
a=a%m;
对于(int x=1;x>num[i];
对于(inti=0;i>rem[i];
infle.close();
int k=sizeof(num)/sizeof(num[0]);
流文件;
打开(“output.txt”);
myfile这些行:

for(int i=1;i
a=a%m;
--
返回结果%prod;
--
m
prod
的值是什么?另外,
#include
--去掉该行。prod只是一个变量prod=1,就像m=m1*m2*m3*.m(n)。我们需要m的值,所以我用prod代替m表示mx==a(mod m)我正在获取input.txt文件。您是否验证了这些值?我没有看到您在代码中检查它们是否为
0
findMinX(num,rem,k)
是可疑的。我希望
findMinX(num,rem,n)
。在
findMinX
中,我希望循环是从
0
@YogeshSIngh我的回答有什么意义吗?如果您对此有任何疑问,请随时提问。
int ModInverse(int a, int m)
{
    a = a % m;
    for(int x = 1; x < m; x++)
        if(((a * x) % m) == 1) return x;
}
5
1 2 3 4 5
2 3 4 5 6